如何使用SimpleXML解析包含自定义名称空间的XML? [英] How do I parse XML containing custom namespaces using SimpleXML?
问题描述
我想知道如何解析XML名称中似乎带有:的值.我一直在使用:
I'm wondering how to parse values in XML that appear to have : in their name. I've been using:
$response = file_get_contents($url);
$data = simplexml_load_string($response);
然后做一个:
foreach($data->item as $key => $current){
但是,我得到的最新提要之一是提要名称中的冒号,如以下示例所示:
However, one of the latest feeds that I've been given has colons in the name of the feed as seen in the example below:
<item>
<title>foo</title>
<description>foo</description>
<ccc:fid>10</ccc:fid>
<ccc:bid>6</ccc:bid>
</item>
当我尝试创建$ current-> ccc:bid php时不高兴(理应如此).有什么办法可以解决这个问题?
When i try to create a $current->ccc:bid php does not get to happy (rightfully so). Is there any way to get around this?
推荐答案
ccc:fid的使用是命名空间的扩展,需要在xml中声明该命名空间,以便能够在诸如simplexml之类的库中使用它.
the usage of ccc:fid is an extension of the namespace which needs to be declared in xml in order to be able to use it in libraries like simplexml.
以下是命名空间中对us的描述的一些示例:
Here are some examples of descriptions of usin a namespace:
- http://www.w3.org/TR/1999/REC-xml-names-19990114/
- http://www.w3.org/TR/xml-names/
希望有所帮助,尽管有点复杂.
Hope that helps, albeit it is a little complicated.
这篇关于如何使用SimpleXML解析包含自定义名称空间的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!