你好,我有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