如何从字典中获取XML数据

时间:2015-08-31 06:20:56

标签: php xml

以下是XML format

    <item>
                <title>Lorem ipsum</title>
                <link>http://www.website.com</link>
                <pubDate>Sun, 30 Aug 2015 16:51:18 +0000</pubDate>
                <description>
                    <![CDATA[Dummy text Dummy text Dummy text]]>
                </description>

                <title>Lorem ipsum test</title>
                <link>http://www.test.com</link>
                <pubDate>Sun, 29 Aug 2015 16:51:18 +0000</pubDate>
                <description>
                    <![CDATA[Dummy text test Dummy text test Dummy text test]]>
                </description>
    </item>

我尝试了什么:

    <?php
    $rss = file_get_contents('<URL>');
    $xml = new SimpleXMLElement($rss);
    foreach($xml->channel->item as $feed){
        $title = $feed->title;
        $added_date_time = date("Y-m-d H:i:s", strtotime($date));
        $description = $feed->description;
        print_r($title);
        print_r($description);
    }
    ?>

它为描述

提供了此输出
SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )

如何获取描述数据?

1 个答案:

答案 0 :(得分:0)

你可以试试php:s SimpleXML:加载你的XML,然后访问元素

$items = new SimpleXMLElement($xmlstring);
echo $items->description;
编辑:好的,我发现你现在已经在问题中更改了XML,但解决方案仍然有用。代码回应&#34; dymmy ......&#34;在这个沙箱上:http://sandbox.onlinephpfunctions.com/您的描述中缺少某些内容,或者您​​正在运行一个奇怪的系统。您也可以尝试加载sring(应该将cadata对象转换为清理字符串)

$items = simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);

此外,您可以尝试将要获得的元素强制转换为strin:

echo (string) $items->description;
相关问题