Latest web development tutorials

PHP xpath () function

PHP SimpleXML Reference Manual Complete PHP SimpleXML Reference Manual

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)
}

参数 描述
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>

PHP code

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

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

print_r($result);
?>

The code above will output:

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


PHP SimpleXML Reference Manual Complete PHP SimpleXML Reference Manual