从xml feed获取enclosure标记中的URL值

时间:2015-08-17 06:17:50

标签: php xml

这是我要解析的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的值?

1 个答案:

答案 0 :(得分:0)

因为enclosure只是item的子元素,而url属性,所以它看起来与其他元素非常相似项目。您需要attributes才能访问该属性:

'desc' => $node->getElementsByTagName('enclosure')->item(0)->attributes['url']->nodeValue,