我有一个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
}
答案 0 :(得分:-2)
<?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']);