php - xml xpath does not return node value -



php - xml xpath does not return node value -

i have test file i'm trying parse xml string using simplexml's xpath method.

when seek access nodes values straight using xpath empty output, when utilize xpath grab elements , loop through them works fine.

when @ documentation, seems syntax should work. there i'm missing?

<?php $xmlstring = '<?xml version="1.0" encoding="iso-8859-1"?> <users> <user> <firstname>sheila</firstname> <surname>green</surname> <address>2 st</address> <city>campbelltown</city> <country>australia</country> <contact> <phone type="mobile">1234 1234</phone> <url>http://example.com</url> <email>pamela@example.com</email> </contact> </user> <user> <firstname>bruce</firstname> <surname>smith</surname> <address>1 yakka st</address> <city>meekatharra</city> <country>australia</country> <contact> <phone type="landline">4444 4444</phone> <url>http://yakka.example.com</url> <email>bruce@yakka.example.com</email> </contact> </user> </users>'; // start parsing if(!$xml = simplexml_load_string($xmlstring)){ echo "error loading string "; } else { echo "<pre>"; // print firstname values straight xpath // outputs elements, values blank print_r($xml->xpath("/users/user/firstname")); // set variable of user elements , loop through , print firstname values // output values $users = $xml->xpath("/users/user"); foreach($users $user){ echo $user->firstname; } // find firstname values tag // not output values print_r($xml->xpath("//firstname")); echo "</pre>"; }

as per manual http://uk1.php.net/manual/en/simplexmlelement.xpath.php

the xpath method searches simplexml node children matching xpath path.

in first , 3rd examples, beingness returned objects containing array of node's value, rather node itself. aren't going able e.g.

$results = $xml->xpath("//firstname"); foreach ($results $result) { echo $result->firstname; }

instead can echo out value directly. well, straight (they still simplexml objects after all)...

$results = $xml->xpath("//firstname"); foreach ($results $result) { echo $result->__tostring(); }

php xml xpath xml-parsing

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -