【问题标题】:SimpleXML parse Child of a ChildSimpleXML 解析 Child of a Child
【发布时间】:2011-08-21 19:02:53
【问题描述】:

我正在从 Yahoo API 检索一些类似的 XML -

<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>

例如,我将如何访问孩子 woeid

我可以访问质量、经度等,但我有点不确定如何访问孩子的孩子 - 这甚至是正确的术语吗?

任何帮助表示赞赏。

谢谢

【问题讨论】:

    标签: php xml simplexml xml-parsing


    【解决方案1】:
    <?php
    $xml = simplexml_load_file("XML.xml");
    //echo woeid
    echo $xml->Result->woeid;
    ?>
    

    这只是一个简单的示例,说明如何使用您提供的 xml 文件进行操作。

    您可能还会遇到有多个 &lt;Result&gt; 子级的文件,在这种情况下,您可以像这样访问它们:

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

    【讨论】:

    • 谢谢,太好了。我知道这会非常简单!
    • 感谢您更新的答案,它将很有用! :)
    猜你喜欢
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2012-03-07
    • 2013-05-26
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多