这是一个初学者的问题(对不起);我使用simplexml_load_string()php函数来解析RSS提要,我的代码如下所示:
<?php
$url = "http://www.zdnet.com/blog/rss.xml";
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
foreach($feed->channel as $channel) {
echo "Link : " . $channel->link . "<P>";
echo "Image URL : " . $channel->image->url . "<P>";
}
foreach($feed->channel->item as $item) {
echo "<HR><P>";
echo "Link : " . $item->link . "<P>";
echo "Title : " . $item->title . "<P>";
echo "Description : " . $item->description . "<P>";
echo "Date : " . $item->pubDate . "<P>";
}
?>
哪个工作正常,但XML文件有一些看起来像这样的标签:
<media:credit role="author">
<![CDATA[ James Kendrick ]]>
</media:credit>
<media:text type="html">
<![CDATA[
<figure class="alignRight"><a href="/i/story/70/00/034218/kindle-iphone-2.jpg" target="_blank">
<img title="kindle-iphone-2" alt="kindle-iphone-2" src="http://cdn-static.zdnet.com/1.jpg">
height="237" width="200"></a><figcaption>(I
]]>
<![CDATA[
mage: James Kendrick/ ZDNet)</figcaption></figure> <p>Regular readers know I am a tablet guy
]]>
</media:text>
如何解析这些标签?
由于
答案 0 :(得分:0)
要访问具有名称空间的节点,在这种情况下可以使用->children()
:
$url = "http://www.zdnet.com/blog/rss.xml";
$feed = simplexml_load_file($url, null, LIBXML_NOCDATA);
foreach($feed->channel->item as $item) {
echo "<HR><P>";
echo "Link : " . $item->link . "<P>";
echo "Title : " . $item->title . "<P>";
echo "Description : " . $item->description . "<P>";
echo "Date : " . $item->pubDate . "<P>";
$media = $item->children('media', 'http://search.yahoo.com/mrss/'); // medias
// then just loop the children
// foreach($media as $m) {}
$s = $item->children('s', 'http://www.zdnet.com/search'); // ss
}