note 84196 modified in function.simplexml-element-xpath by danbrown

From: Date: Wed, 16 Sep 2009 15:51:21 +0000
Subject: note 84196 modified in function.simplexml-element-xpath by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160679@lists.php.net to get a copy of this message
If you want to find easly all records satisfying some condition in XML data like .... <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> </book> ... try example below <?php $xmlStr = file_get_contents('data/books.xml'); $xml = new SimpleXMLElement($xmlStr); // seach records by tag value: // find all book records with price higher than 40$ $res = $xml->xpath("book/price[.>'40']/parent::*"); print_r($res); ?> You will see response like: Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk101 ) [author] => Gambardella, Matthew [title] => XML Developer's Guide [genre] => Computer [price] => 44.95 [publish_date] => 2000-10-01 [description] => An in-depth look at creating applications with XML. ) ... --was-- If you want to find easly all records satisfying some condition in XML data like .... <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> </book> ... try example below <?php $xmlStr = file_get_contents('data/books.xml'); $xml = new SimpleXMLElement($xmlStr); // seach records by tag value: // find all book records with price higher than 40$ $res = $xml->xpath("book/price[.>'40']/parent::*"); print_r($res); ?> You will see response like: Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk101 ) [author] => Gambardella, Matthew [title] => XML Developer's Guide [genre] => Computer [price] => 44.95 [publish_date] => 2000-10-01 [description] => An in-depth look at creating applications with XML. ) ... Nice xpath tutorial you can find on http://www.zvon.org/xxl/XPathTutorial/General/examples.html http://php.net/manual/en/function.simplexml-element-xpath.php

« previous php.notes (#160679) next »