你好,我有XML格式的一系列项目,如该API响应:PHP DOM文档获得属性
<item>
<title>blah balh</title>
<pubDate>Tue, 20 Oct 2009 </pubDate>
<media:file date="today" data="example text string"/>
</item>
我想使用的DOMDocument获取属性“数据”从标签“媒体:文件”。我的下面的尝试不起作用:
$xmldoc = new DOMDocument();
$xmldoc->load('api response address');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem) {
$nodes = $feeditem->getElementsByTagName('media:file');
$linkthumb = $nodes->item(0)->getAttribute('data');
}
我在做什么错?请帮忙。
编辑:我不能因为某些原因留下评论马克。我得到错误
Call to a member function getAttribute() on a non-object
当我运行我的代码。我也试过
$nodes = $feeditem->getElementsByTagNameNS('uri','file');
$linkthumb = $nodes->item(0)->getAttribute('data');
其中uri是与媒体名称空间(NS)有关的uri,但又是同样的问题。
请注意,媒体元素的形式不是我认为这是问题的一部分,因为我通常没有问题解析attibutes。
也做到了,额外的代码。 谢谢! – 2009-10-23 22:25:44