PHP xpath () function
Definition and Usage
xpath () function runs on XPath XML document query.
If successful, the function returns an array SimpleXMLElements objects. If it fails, it returns FALSE.
grammar
class SimpleXMLElement
{
string xpath(path)
}
{
string xpath(path)
}
参数 | 描述 |
---|---|
path | 必需。规定要在 XML 文档中搜索的 XPath 路径。 |
Examples
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>
<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);
?>
$xml = simplexml_load_file("test.xml");
$result = $xml->xpath("from");
print_r($result);
?>
The code above will output:
Array
(
[0] => SimpleXMLElement Object
(
[0] => Jani
)
)
(
[0] => SimpleXMLElement Object
(
[0] => Jani
)
)
Complete PHP SimpleXML Reference Manual