【问题标题】:xml data upon attribute基于属性的 xml 数据
【发布时间】:2013-06-18 10:43:02
【问题描述】:

如何获取属性名称上以下 xml 的值。

    <list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="165">
<resource classname="Quote">
<field name="name">USD/KRW</field>
<field name="price">1131.319946</field>
<field name="symbol">KRW=X</field>
<field name="ts">1371547710</field>
<field name="type">currency</field>
<field name="volume">0</field>
</resource>
<resource classname="Quote">
<field name="name">SILVER 1 OZ 999 NY</field>
<field name="price">0.045962</field>
<field name="symbol">XAG=X</field>
<field name="ts">1371505422</field>
<field name="type">currency</field>
<field name="volume">7</field>
</resource>....

有165个这样的结构

想要获取 银 1 盎司 999 纽约 0.045962 XAG=X 1371505422等

到目前为止我的代码是这样的

$xml = simplexml_load_string($data);

foreach($xml->children() as $resources)
{
    foreach($resources->children() as $resource => $data)
    {
        echo $data->field['name'];
        echo "<br>";
    }


}

【问题讨论】:

    标签: php xml


    【解决方案1】:

    根据评论中的XML,应该是:

    $xml = simplexml_load_string($data);
    
    foreach($xml->resources->resource as $resource)
    {
        foreach($resource->field as $field)
        {
            echo $field->attributes()->name; // e.g. name, price, symbol
            echo (string)$field; // this is the content, e.g. SILVER 1 OZ 999 NY
        }
    }
    

    请注意,$xml 始终包含根元素,在本例中为 &lt;list&gt;

    Demo

    【讨论】:

    • resource-listUSD/KRW1131.319946KRW=X1371547710货币0SILVER 1 OZ 999 NY0.045962XAG=X1371505422货币 .....
    • 它给了我一个空白输出。屏幕上什么都没有显示!根据你修改的代码。
    • 是的,它完成了。万分感谢。我快疯了……谢谢。
    【解决方案2】:

    给你。

    foreach($xml->children() as $resources)
    {
    foreach($resources->children() as $resource => $data)
    {
        echo $data->field['name'];
    
        echo "<br>";
        echo $resource->attributes()->name;
    
    }
    
    
    }
    

    【讨论】:

    • 致命错误:在非对象上调用成员函数 attributes()
    【解决方案3】:

    更正的代码:

    <quote>
    <name>test</name><price>567</price><symbol>xyz</symbol><ts>1371505422</ts>
    <type>currency</type>
    <volume>7</volume>
    </quote>
    $xmlObject = new SimpleXMLElement($xmlstring);
    foreach ($xmlObject->children() as $node){
    echo $node->Company;
    echo $node->price;
    echo $node->symbol;
    echo $node->ts;
    echo $node->type;
    echo $node->volume;
    }
    

    【讨论】:

      【解决方案4】:

      你并没有那么远。按名称查询元素而不是使用children(),您只需要children() 方法用于不在元素命名空间内的子元素。在您的代码中,您刚刚查询了错误的数据,请参见此示例 (Demo):

      $xml = simplexml_load_string($data);
      
      foreach($xml->resources->resource as $resource)
      {
          echo "---------------\n";
          foreach($resource->field as $field)
          {
              echo $field['name'], ': ',  // e.g. name, price, symbol
                   $field, "\n";          // this is the content, e.g. SILVER 1 OZ 999 NY
          }
      }
      

      输出:

      ---------------
      name: USD/KRW
      price: 1131.319946
      symbol: KRW=X
      ts: 1371547710
      type: currency
      volume: 0
      ---------------
      name: SILVER 1 OZ 999 NY
      price: 0.045962
      symbol: XAG=X
      ts: 1371505422
      type: currency
      volume: 7
      

      这在基本的 simplexml 使用示例中进行了概述和解释,请参阅:http://php.net/simplexml.examples-basic

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多