PHP:Simplexml解析节点

时间:2014-05-14 17:17:32

标签: php simplexml

我有一个用于解析xml数据的PHP代码,但它不能正常工作,我的php代码如下:

<?php
            $xml = '<?xml version="1.0" encoding="utf-8"?>
                <videos>
                    <video>
                        <id>751985</id>
                        <embed><![CDATA[somehtmlcode]]></embed>
                        <thumbs>
                            <thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum02.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum03.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum04.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum05.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum06.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum07.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum08.jpg></thumb>
                        </thumbs>
                        <link><![CDATA[http://example.com/001]]></link>
                        <title><![CDATA[Some cool title 1]]></title>
                        <categories>
                            <category><![CDATA[Cat1]]></category>
                            <category><![CDATA[Cat2]]></category>
                            <category><![CDATA[Cat3]]></category>
                            <category><![CDATA[Cat4]]></category>
                        </categories>
                        <tags>
                            <tag><![CDATA[tag1]></tag>
                            <tag><![CDATA[tag2]></tag>
                            <tag><![CDATA[tag3]></tag>
                        </tags>
                        <someone>
                        </someone>
                        <duration><![CDATA[8:17]]></duration>
                        <pubDate><![CDATA[2014-05-14]]></pubDate>
                    </video>
                    <video>
                        <id>751988</id>
                        <embed><![CDATA[somehtmlcode]]></embed>
                        <thumbs>
                            <thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum02.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum03.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum04.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum05.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum06.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum07.jpg></thumb>
                            <thumb><![CDATA[http://example.com/images/thum08.jpg></thumb>
                        </thumbs>
                        <link><![CDATA[http://example.com/001]]></link>
                        <title><![CDATA[Some cool title 1]]></title>
                        <categories>
                            <category><![CDATA[Cat1]]></category>
                            <category><![CDATA[Cat2]]></category>
                            <category><![CDATA[Cat3]]></category>
                            <category><![CDATA[Cat4]]></category>
                        </categories>
                        <tags>
                            <tag><![CDATA[tag1]></tag>
                            <tag><![CDATA[tag2]></tag>
                            <tag><![CDATA[tag3]></tag>
                        </tags>
                        <someone>
                        </someone>
                        <duration><![CDATA[8:17]]></duration>
                        <pubDate><![CDATA[2014-05-14]]></pubDate>
                    </video>
                </videos>';



        $sxe = simplexml_load_string($xml);     

        foreach ($sxe->video as $videoChild) {
        echo $videoChild->id; 
        echo $videoChild->title;


        }

?>

它没有得到节点的值,我需要获取id,嵌入,拇指,链接,标题,没有CDATA标记。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

 <thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>

[CDATA]事情没有正确关闭,simplexml_load_string()不喜欢

 <thumb><![CDATA[http://example.com/images/thum01.jpg]]></thumb>