Take the 2-minute tour ×

I am retrieving some similar XML from the Yahoo API -

<ResultSet version="1.0">
  <ErrorMessage>No error</ErrorMessage>
     <name>51.501690392606974, -0.1254415512084961</name>

How would I go about accessing the child woeid for example?

I can access quality, longitude etc but I am a little unsure as to how to access a child of a child - is this even the correct terminology?

Any help appreciated.


share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
$xml = simplexml_load_file("XML.xml");
//echo woeid
echo $xml->Result->woeid;

That's just an easy example of how to do it with the xml-file you provided.

You might also run into files where there's more than one <Result> child, in which case you can access them all like this:

$xml = simplexml_load_file("XML.xml");
//echo all woeid's
foreach($xml->Result as $result) {
    echo $result->woeid;
share|improve this answer
Thank you, that's brilliant. I knew it would be something amazingly simple! –  Tom Aug 21 '11 at 19:13
Glad i could help. ;) –  Kristoffer la Cour Aug 21 '11 at 19:15
and thanks for the updated answer, it will come in useful! :) –  Tom Aug 21 '11 at 19:23
add comment

Your Answer


