如何从标签中的XML获取2个CDATA值?

时间:2021-08-17 08:14:25

I am using codeigniter framework, I have a xml file with below code:

我正在使用codeigniter框架,我有一个带有以下代码的xml文件:

<rss xmlns:content="" xmlns:wfw="" xmlns:dc="" xmlns:atom="" xmlns:sy="" xmlns:slash="" version="2.0">
    <channel>
        <item>
            <title>Title1</title>
            <link>Link</link>
            <pubDate>Date</pubDate>
            <content:encoded>
                <![CDATA[ This is description 1 ]]>
                <![CDATA[ This is description 2 ]]>
            </content:encoded>
        </item>

        <item>
            <title>Title2</title>
            <link>Link2</link>
            <pubDate>Date2</pubDate>
            <content:encoded>
                <![CDATA[ This is description 3 ]]>
                <![CDATA[ This is description 4 ]]>
            </content:encoded>
        </item>
</channel>
</rss>

I tried this to parse the xml:

我试过这个来解析xml:

<?php
function test()
{
    $xml = simplexml_load_file('http://localhost/testxml/testxml.xml','SimpleXMLElement',LIBXML_NOCDATA);
    echo "<pre>";
    print_r($xml);
    echo "</pre>";
}
?>

It is giving me output without the CDATA which is exists in this tag content:encoded:

它给出了我没有CDATA的输出,该CDATA存在于此标记内容中:encoded:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => Title1
                            [link] => Link
                            [pubDate] => Date
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [title] => Title2
                            [link] => Link2
                            [pubDate] => Date2
                        )

                )

        )

)

How can I get <content:encoded><![CDATA[ This is description 1 ]]><![CDATA[ This is description 2 ]]></content:encoded> this data in parsing?

我怎样才能得到 解析这个数据?

1 个解决方案

#1


0  

I got the answer too, What i did is:

我也得到了答案,我做的是:

<?php
function getFeed() {
        $feed_url = 'http://localhost/testxml/testxml.xml';
        $feeds = file_get_contents($feed_url);
        $feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds);
        $feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds);
        $rss = simplexml_load_string($feeds);

        foreach($rss->channel->item as $entry) {
            echo ("<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>");
            echo ("$entry->contentEncoded");
        }
    }
?>

#1


0  

I got the answer too, What i did is:

我也得到了答案,我做的是:

<?php
function getFeed() {
        $feed_url = 'http://localhost/testxml/testxml.xml';
        $feeds = file_get_contents($feed_url);
        $feeds = str_replace("<content:encoded>","<contentEncoded>",$feeds);
        $feeds = str_replace("</content:encoded>","</contentEncoded>",$feeds);
        $rss = simplexml_load_string($feeds);

        foreach($rss->channel->item as $entry) {
            echo ("<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>");
            echo ("$entry->contentEncoded");
        }
    }
?>