2013-01-31 44 views
0

我正在尝试阅读和解析RSS提要。 RSS提要的标题是:如何获取getElementsByTagNameNS内容?

<rss version="2.0" xmlns:content="http://www.mywebsite.com" xmlns:media="http://search.yahoo.com/mrss/" > 

我成功地检索既<title><link>从项目,但我似乎无法找到一种方法来检索<content:encoded>孩子的。这就是我tryed至今:

<?php 

//error_reporting(E_ALL); 
//ini_set("display_errors", 1); 

.... 

$x=$xmlDoc->getElementsByTagName('item'); 

foreach($x as $item) { 
    $link = $item->getElementsByTagName('link')->item(0)->nodeValue; 
    $title = $item->getElementsByTagName('title')->item(0)->nodeValue; 
    $content = $item->getElementsByTagNameNS('http://www.mywebsite.com', 'encoded')->item(0); 
    echo ("<p><a href='" . $link. "'>" . $title . "</a></p><p>" . $content . "</p>"); 
} 

?> 

你可以想像$content是空的,即使我试图让nodeValue属性,我缺少什么吗?

+0

你需要reeinvent轮?为什么不使用SimplePie来解析RSS? –

+0

我的服务器位于代理之后,SimplePie允许我在请求期间设置它吗?无论如何,我在这里使用DomDocument,我不认为我是在重新发明轮子。 – raz3r

+0

当我说重新发明轮子时,我的意思是你需要从头开始编写所有的代码才能获得这些值。我从来没有用代理测试SimplePie,但我认为这是可能的。看看它[链接](http://www.simplepie.org/) –

回答

1

我最终使用的SimpleXMLElement:

$x = new SimpleXmlElement($s); 

foreach($x->channel->item as $entry) { 

    $content = $entry->children("content", true); 
    echo "<p>".$content->encoded."</p>"; 
} 

就像一个魅力。

1

这里的标签名称是“编码”。

只需使用

$content => $node->getElementsByTagName('encoded')->item(0)->nodeValue