解析XML与PHP包含名称空间
问题描述:
我试图解析XML文件中的PHP看起来像:解析XML与PHP包含名称空间
<post dsq:id="##Edited out##">
<id />
<message>##Edited out##</message>
<createdAt>2010-07-09T16:08:44Z</createdAt>
<author>
<email>##Edited out##</email>
<name>##Edited out##</name>
<isAnonymous />
</author>
<ipAddress>##Edited out##</ipAddress>
<thread dsq:id="##Edited out##" />
</post>
<post dsq:id="##Edited out##">
<id />
<message>##Edited out##</message>
<createdAt>2010-07-09T16:10:07Z</createdAt>
<author>
<email>##Edited out##</email>
<name>##Edited out##</name>
<isAnonymous />
</author>
<ipAddress>##Edited out##</ipAddress>
<thread dsq:id="##Edited out##" />
</post>
我使用简单的XML解析它是这样的:
$xml = simplexml_load_file('disqus.xml');
foreach ($xml->post as $post)
{
echo $post['dsq:id'];
}
然而,我无法从后对象这里的id属性:
<post dsq:id="##Edited out##">
任何信息怎么会被检索会不胜感激。
答
$xml = simplexml_load_file('disqus.xml');
foreach ($xml->post as $post)
{
$dsq = $post->children('disqus.com/disqus-internals'); //should this not have http://disqus.com/disqus-internals?
echo $dsq->id;
}
您正在使用的那个w3.org/2001/XMLSchema-instance
用于xsi命名空间。
命名空间应该位于文档根目录下。它是什么? – Gordon 2011-03-29 21:50:53
dsq?也许可以吗? – Treffynnon 2011-03-29 21:53:06