How to query the <entry> nodes from an ATOM feed

↘锁芯ラ 提交于 2019-12-13 04:04:49

问题


I can query the description nodes in an RSS feed and return a result like this:

$xpath = new DOMXPath($xmlDoc);
$items = $xpath->query('/rss/channel/item/description/..');

foreach($items as $number => $item){}

But it returns nothing when I query the entry nodes from an Atom feed like this (in which I follow the same pattern as RSS's above):

$xpath = new DOMXPath($xmlDoc);
$items = $xpath->query('/feed/entry/..');

foreach($items as $number => $item){}

What am I missing?


回答1:


There is no RSS namespace, whereas Atom elements are in the following namespace:

http://www.w3.org/2005/Atom 

You can see this by looking at your document's feed element, which probably looks something like this:

<feed xmlns="http://www.w3.org/2005/Atom">

You need to register this namespace before querying for elements in it:

$xpath->registerNamespace('a', 'http://www.w3.org/2005/Atom'); 

And then use the chosen namespace prefix in your expression:

$items = $xpath->query('/a:feed/a:entry');


来源:https://stackoverflow.com/questions/8068997/how-to-query-the-entry-nodes-from-an-atom-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!