read cdata from a rss feed

十年热恋 提交于 2019-11-27 08:38:56

问题


I'm reading a rss feed using simple code:

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

and the output like this: SimpleXMLElement Object ( [@attributes] => Array ( [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
                            (
                            )

                    )

and more.... but when I View source of this page I have Info like this:

 <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>

how can I read and store CDATA value in mydatabase .


回答1:


Tell SimpleXML to convert CDATA into normal texts:

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

That should do it for you, using simplexml_load_file instead of file_get_contents.

Related Answer: Removing cdata in simplehtmldom.




回答2:


The above "fix" will work, but is entirely unnecessary.

SimpleXML objects contain a lot of "magic", and are not designed to be viewed using print_r; the CDATA is safely in your object, but won't show up unless you ask for it in the right way.

If you run echo (string)$movies->channel->title; you should get "Forbes.com: News" as you would expect.

Note the (string), which tells PHP to explicitly convert the "magic" SimpleXMLElement into a string. If you don't do this, you'll actually be getting another SimpleXMLElement object back - otherwise my example wouldn't work because $movies->channel would be a string.

It's good practice to always use (string) when accessing elements or attributes from SimpleXML, as some functions will choke if they are expecting a string and you give them a SimpleXML object instead, and serializing or session storage will certainly fail.



来源:https://stackoverflow.com/questions/8020181/read-cdata-from-a-rss-feed

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