关于simplexml无法加载CDATA中数据的问题

关于simplexml无法加载CDATA中数据的问题

<?php

$xmlstr = <<<EOF

<?xml version="1.0" encoding="UTF-8"?>

<MusicFeeds>

<Songs Action="Add">

<Name type="Native"><![CDATA[不装饰]]></Name>

<Artist><![CDATA[王菲]]></Artist>

<PRODUCTID>jwadyqjsge3dccbogr</PRODUCTID>

</Songs>

<Songs Action="Add">

<Name type="Native"><![CDATA[又继续等]]></Name>

<Artist><![CDATA[王菲]]></Artist>

<PRODUCTID>jwadyqjsge3dccboge</PRODUCTID>

</Songs>

<Songs Action="Add">

<Name type="Native"><![CDATA[只有你]]></Name>

<Artist><![CDATA[王菲]]></Artist>

<PRODUCTID>jwadyqjsge3dccbogc</PRODUCTID>

</Songs>

</MusicFeeds>

EOF;

$xml = simplexml_load_string($xmlstr);

var_dump($xml);

?>

xml文件的节点中如果含有CDATA,用simplexml_load_string或simplexml_load_file函数读取里面的数据是读取不出来的(不管是中文还是字母或字符),我自己亲自测试过了。

SimpleXMLElement Object

(

[Songs] => Array

(

[0] => SimpleXMLElement Object

(

[@attributes] => Array

(

[Action] => Add

)

[Name] => SimpleXMLElement Object

(

[@attributes] => Array

(

[type] => Native

)

)

[Artist] => SimpleXMLElement Object

(

)

[PRODUCTID] => jwadyqjsge3dccbogr

)

[1] => SimpleXMLElement Object

(

[@attributes] => Array

(

[Action] => Add

)

[Name] => SimpleXMLElement Object

(

[@attributes] => Array

(

[type] => Native

)

)

[Artist] => SimpleXMLElement Object

(

)

[PRODUCTID] => jwadyqjsge3dccboge

)

[2] => SimpleXMLElement Object

(

[@attributes] => Array

(

[Action] => Add

)

[Name] => SimpleXMLElement Object

(

[@attributes] => Array

(

[type] => Native

)

)

[Artist] => SimpleXMLElement Object

(

)

[PRODUCTID] => jwadyqjsge3dccbogc

)

)

)

如果将代码改为

<?php

$xmlstr = uncdata($xmlstr);

$xml = simplexml_load_string($xmlstr);

var_dump($xml);

?>

则cdata里面的数据可以正常输出了。