du12197 2014-05-14 17:17
浏览 47
已采纳

PHP:Simplexml解析节点

I have a php code that use to parse xml data, but it is not working, my php code look as following:

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


        }

?>

it does not get the value of node, i need to get id, embed, thumb, link, title, without the CDATA tag. any help would be great.

  • 写回答

1条回答 默认 最新

  • dongxie3963 2014-05-14 17:30
    关注
     <thumb><![CDATA[http://example.com/images/thum01.jpg></thumb>
    

    the [CDATA] things aren't closed properly, simplexml_load_string() doesn't like that

    try

     <thumb><![CDATA[http://example.com/images/thum01.jpg]]></thumb>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,