php - 从 rss 提要中读取 cdata

标签 php xml cdata

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

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

输出如下: SimpleXMLElement 对象 ( [@attributes] => 数组 ( [版本] => 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

相关答案:Removing cdata in simplehtmldom .

关于php - 从 rss 提要中读取 cdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020181/

相关文章:

javascript - 如何从 Node Sax 中的 XML 文件读取 CDATA

PHP:如何在数据库表中插入动态检索的数据

iphone - iPhone 上的 XML 和 SQLite 内存利用率和性能

.net - 如何加快 WCF 客户端的启动性能

xml - laravel 下载一个新创建的 xml 文件

java - 通过 DOM 解析器从 XML 处理 CDATA

php - Mysql:比较无法正常工作

php - MySQL 加入一张表。使用 where 子句列出值,然后在下方列出值

php 准备好的语句 ||语法错误或访问冲突 : 1064

Delphi、soap 和将值包装在 cdata 中