Run an XPath query on the XML document in PHP

Description

The following code shows how to run an XPath query on the XML document.

Example


  /*from  ww w  . j av a  2s .co m*/
  
  
<?php
    $xml = simplexml_load_file("test.xml");
    
    $result = $xml->xpath("from");
    
    print_r($result);

?> 

The following code is for test.xml


//from w ww.j a v  a  2 s .  com
    <?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Work</to>
    <from>Home</from>
    <heading>Reminder</heading>
    <body>This is a message.</body>
    </note> 

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser