开发者

SimpleXML parse Child of a Child

I am retrieving some similar XML from the Yahoo API -

<ResultSet version="1.0">
  <Error>0</Error>
  <ErrorMessage>No error</ErrorMessage>
  <Locale>us_US</Locale>
  <Quality>99</Quality>
  <Found>1</Found>
  <Result> 
     <quality>99</quality>
     <latitude>51.501690</latitude>
     <longitude>-0.125442</longitude>
     <offsetlat>51.501690</offsetlat>
     <offsetlon>-0.125442</offsetlon>
     <radius>500</radius>
     <name>51.501690392606974, -0.1254415512084961</name>
     <woeid>26352062</woeid>
  </Result>
</ResultSet>

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.

Than开发者_Go百科ks


<?php
$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:

<?php
$xml = simplexml_load_file("XML.xml");
//echo all woeid's
foreach($xml->Result as $result) {
    echo $result->woeid;
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜