【问题标题】:How to get PHP simple XML value with child node?如何使用子节点获取 PHP 简单 XML 值?
【发布时间】:2014-02-20 20:06:58
【问题描述】:

我有一个如图所示的 XML。我需要遍历每个循环的标签并提取属性attr1。但我不断收到"Illegal string offset"

我的数据:

<Parent>
  <Child>
    <ID attr1="123" attr2="567">abc</ID>
    <ID attr1="123" attr2="567">abc</ID>
  </Child>
</Parent>

我的代码:

foreach ($parentNode->Child->ID as $IDChild => $value)
{
    echo $value; // does this output "abc"?
    echo $IDChild['attr1']; // need to display "123", but throwing error.
    $IDChild['attr1'] = "999"; // also need to update the value in the xml
}

【问题讨论】:

    标签: php xml


    【解决方案1】:
    <?php
    $oXml = new SimpleXMLElement('<Parent>
      <Child>
        <ID attr1="123" attr2="567">abc</ID>
        <ID attr1="123" attr2="567">abc</ID>
      </Child>
    </Parent>');
    
    foreach($oXml->Child->ID as $oXml)
        var_dump((string)$oXml['attr1']);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 2013-04-15
      • 2021-10-23
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      相关资源
      最近更新 更多