解析xml文件时获取cdata内容 [英] Getting cdata content while parsing xml file

查看:1397
本文介绍了解析xml文件时获取cdata内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <events date="01-10-2009" color="0x99CC00" selected="true"> 
       <event>
            <title>You can use HTML and CSS</title>
            <description><![CDATA[This is the description ]]></description>
        </event>
    </events>
</xml>

我使用xpath和xquery来解析xml.

I used xpath and and xquery for parsing the xml.

$xml_str = file_get_contents('xmlfile');
$xml = simplexml_load_string($xml_str);
if(!empty($xml))
{
    $nodes = $xml->xpath('//xml/events');
}

我正确获得标题,但没有获得描述.我如何获取内部数据 cdata

i am getting the title properly, but iam not getting description.How i can get data inside the cdata

推荐答案

SimpleXML的CDATA有点问题,因此请使用:

SimpleXML has a bit of a problem with CDATA, so use:

$xml = simplexml_load_file('xmlfile', 'SimpleXMLElement', LIBXML_NOCDATA);
if(!empty($xml))
{
    $nodes = $xml->xpath('//xml/events');
}
print_r( $nodes );

这将为您提供:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [date] => 01-10-2009
                    [color] => 0x99CC00
                    [selected] => true
                )

            [event] => SimpleXMLElement Object
                (
                    [title] => You can use HTML and CSS
                    [description] => This is the description 
                )

        )

)

这篇关于解析xml文件时获取cdata内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆