simplexml_load_file只返回xml的部分数据

时间:2014-07-02 04:35:23

标签: php xml

我使用simplexml_load_file并期望它可以在url上使用,奇怪的是有些数据丢失了。

我的php

$url = "http://www.thestar.com.my/RSS/Lifestyle/Women/";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);

部分结果

    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [guid] => {936842CF-550D-4CAB-A840-23BB4E766B8E}
                    [link] => http://www.thestar.com.my/Lifestyle/Women/Fashion/2014/07/02/Jamy-Yang-Mingjie-Shared-travelling-values
                    [title] => SimpleXMLElement Object
                        (
                        )

                    [description] => SimpleXMLElement Object
                        (
                        )

                    [pubDate] => Wed, 02 Jul 2014 00:00:00 +08:00
                    [enclosure] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/JamyYangMingjie020714.ashx?crop=1&w=460&h=345&
                                    [length] => 
                                    [type] => image/jpeg
                                )

                        )

                )

item->title成了SimpleXMLElement对象?

2 个答案:

答案 0 :(得分:1)

当我查看该URL的源代码时,所有字符串都包含CDATA标记,包括标题。根据{{​​3}}

的“wouter at code-b dot nl”

“要从CDATA中正确提取值,请确保使用强制转换运算符将SimpleXML元素转换为字符串值”

答案 1 :(得分:-1)

您忘记添加其他参数以获取CDATA字符数据。试试这个:

$url = "http://www.thestar.com.my/RSS/Lifestyle/Women/";
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";
print_r($xml);