从RSS提要中读取数据


read cdata from a rss feed

我正在使用简单的代码阅读rss提要:

 <?php
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
$movies = new SimpleXMLElement($homepage);
echo '<pre>';
print_r($movies);
?>

,输出如下:SimpleXMLElement对象([@attributes] =>数组([version] => 2.0)

[channel] => SimpleXMLElement Object
    (
        [title] => SimpleXMLElement Object
            (
            )
        [link] => SimpleXMLElement Object
            (
            )
        [description] => SimpleXMLElement Object
            (
            )
        [language] => en-us
        [copyright] => Copyright 2009 Forbes.com LLC
        [item] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [title] => SimpleXMLElement Object
                            (
                            )
                        [link] => SimpleXMLElement Object
                            (
                            )
                        [author] => SimpleXMLElement Object
                            (
                            )
                        [pubDate] => Sat, 05 Nov 2011 07:17:21 GMT
                        [description] => SimpleXMLElement Object
                            (
                            )
                    )

和更多…但是当我查看这个页面的来源时,我有这样的信息:

 <rss version="2.0"><channel><title><![CDATA[Forbes.com: News]]></title><link><!   [CDATA[http://www.forbes.com]]></link><description><![CDATA[News and reports from Forbes.com]]></description><language>en-us</language><copyright>Copyright 2009 Forbes.com LLC</copyright><item><title><![CDATA[Benicio Del Toro Offered Villain Role In "Star Trek" Sequel - Is It Khan?]]></title><link><![CDATA[http://www.forbes.com/sites/markhughes/2011/11/05/benicio-del-toro-offered-villain-role-in-star-trek-sequel-is-it-khan/?feed=rss_home]]></link><author><![CDATA[Mark Hughes]]></author><pubDate>Sat, 05 Nov 2011 07:17:21 GMT</pubDate><description><![CDATA[Variety reports that actor Benicio del Toro is being offered the role of villain in the upcoming sequel to director J.J. Abram?s 2009 blockbuster franchise-reboot movie Star Trek. So far, Abrams and crew have kept a tight lid on details about the new Paramount film, and the identity of the main villain is a closely ...]]></description>
如何在mydatabase中读取和存储CDATA值

告诉SimpleXML将CDATA转换为普通文本:

$homepage = 'http://www.forbes.com/news/index.xml';
$movies = simplexml_load_file($homepage, "SimpleXMLElement", LIBXML_NOCDATA);

simplexml_load_file代替file_get_contents就可以了。

相关答案:删除simplehtmldom中的cdata

上面的"修复"可以工作,但完全没有必要。

SimpleXML对象包含很多"魔法",并不是设计为使用print_r查看;CDATA在你的对象中是安全的,但是除非你以正确的方式请求它,否则它不会显示。

如果你运行echo (string)$movies->channel->title;,你应该得到"Forbes.com: News"如你所料。

注意(string),它告诉PHP显式地将"魔法"SimpleXMLElement转换为字符串。如果你不这样做,你实际上会得到另一个SimpleXMLElement对象返回-否则我的例子将无法工作,因为$movies->channel将是一个字符串。

在访问SimpleXML中的元素或属性时,总是使用(string)是很好的做法,因为如果某些函数期望字符串而您给它们一个SimpleXML对象,则会阻塞,并且序列化或会话存储肯定会失败。