Parse XML with a colon attribute in PHP

半世苍凉 提交于 2019-12-23 19:53:34

问题


I'd have the following XML structure:

<entry>
    <id im:id="595831580">blabla</id>
</entry>

Now I want to parse the id 595831580.

I tried:

$idAtt = $xml->entry->id;
$id = $idAtt->attributes();
$id2 = $id['im:id'];

But this does not work :(

How can I fix it?


回答1:


Alright.

You can't use namespace in offsetGet method of SimpleXMLElement, but you can in attributes method:

echo $xml->entry->id->attributes("im",TRUE)->id;

Check out this comment for one more demo.




回答2:


Not sure but maybe something like this

$attr = $xml->id[0]->attributes();
echo $attr['im:id'];


来源:https://stackoverflow.com/questions/15546261/parse-xml-with-a-colon-attribute-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!