PHP Tutorial - PHP xpath() Function






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

Syntax

  class SimpleXMLElement{
   string xpath(path)
}

Parameter

ParameterIs requiredDescription
pathRequired.What to search for in the XML document




Return

This function returns an array of SimpleXMLElements on success, and FALSE of failure.

Example

<?xml version="1.0" encoding="ISO-8859-1"?>
<book>
    <name>PHP</name>
    <name>Java</name>
</book>

PHP Code


<?php
    $xml = simplexml_load_file("test.xml");
    
    $result = $xml->xpath("name");
    
    print_r($result);
?>