PHP XML file filter on match -


i having heck of time getting working...

what want filter xml file city (or market in case).

this xml data.

<itemset> <item> <id>2171</id> <market>vancouver</market> <url>http://</url></item> <item> <id>2172</id> <market>toronto</market> <url>http://</url></item> <item> <id>2171</id> <market>vancouver</market> <url>http://</url></item> 

this code...

<?php $source = 'get-xml-feed.php.xml';  $xml = new simplexmlelement($source);  $result = $xml->xpath('//item/[contains(market, \'toronto\')]');  while(list( , $node) = each($result)) {     echo '//item/[contains(market, \'toronto\')]',$node,"\n"; }  ?> 

if can working access each element, item[0], item[1] base on filtered results.

thanks

i think implements looking using xpath:

<?php $source = file_get_contents('get-xml-feed.php.xml');  $xml = new simplexmlelement($source);  foreach ($xml $node) {     $row = simplexml_load_string($node->asxml());     $result = $row->xpath("//item/market[.='toronto']");     if ($result[0])     {         var_dump($row);     } }  ?> 

as answer mentioned, unless wed use of xpath it's more trouble it's worth application: load xml , treat result array.


Comments