这是我要解析的XML:
<item>
<title>HomeAdvisor finds that there</title>
<link>http://feeds.inc.com/~r/home/updates/story01.htm</link>
<description>The company once called ServiceMagic tried to be everything to everyone looking for a service.</description>
<enclosure url="http://www.inc.com/uploaded_files/image/640x290/getty_475902405_9706479704500113_63918.jpg" type="image/jpeg" />
<pubDate>Mon, 17 Aug 2015 02:40:00 GMT</pubDate>
</item>
这是我的PHP代码:
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->enclosure['url'],
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
);
}
但是,我总是得到错误:
Undefined property: DOMElement::$enclosure
如何在没有任何错误的情况下从enclosure标记中获取URL的值?
答案 0 :(得分:0)
因为enclosure
只是item
的子元素,而url
是属性,所以它看起来与其他元素非常相似项目。您需要attributes
才能访问该属性:
'desc' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue,