Friday, 14 December 2012

PHP - XML: Filtering XML Nodes with XPath

PHP XML Tips - Part 10: Do you want to show text from particular nodes? You can use XPath to filter XML nodes. Like this:
1    <?php
2    $xml = simplexml_load_file("books.xml")
3           or die("Error: Cannot create object");
4    // we want to show just <title> nodes
5    foreach($xml->xpath('//title') as $title){
6      echo $title. "<br />";
7    }
8    ?>

Books.xml, you can see at this.

Result, like this:

No comments:

Post a Comment