PHP xpath()Function


PHP SimpleXML 参考手册Complete PHP SimpleXML Reference Manual

Definition and Usage

The xpath() function runs an XPath query on an XML document.

If successful, the function returns an array of SimpleXMLElements objects. If it fails, it returns FALSE.


Syntax

class SimpleXMLElement
{
string xpath(path)
}

Parameters Description
path Required. Specifies the XPath path to search for in the XML document.


Example

XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

PHP code

<?php
$xml = simplexml_load_file("test.xml");

$result = $xml->xpath("from");

print_r($result);
?>

The above code will output:

Array
(
[0] => SimpleXMLElement Object
(
[0] => Jani
)
)


PHP SimpleXML 参考手册Complete PHP SimpleXML Reference Manual Other Extensions