Cannot parse CDATA with SimpleXML

女生的网名这么多〃 提交于 2019-12-23 19:26:11

问题


I have the following XML code:

<para>
      <![CDATA[
        <?php
        $data = '<?xml version="1.0"?>
        <root>content</root>';
        $sxe = simplexml_load_string($data);
        var_dump($sxe);
        ?>
      ]]>
    </para>

I want to parse the CDATA section to take this result:

Content:
<?php
$data = '<?xml version="1.0"?>
<root>content</root>';
$sxe = simplexml_load_string($data);
var_dump($sxe);
?>

I use the following but it doesn't work.

$xml='sxml.xml';
$book = simplexml_load_file($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

$para = $book->chapter->para[1];
print "Content: ".$para."<br>";

foreach($para AS $node) {
    print "Iter Content: ".$node."<br>";    
}

This results in:

Content: 


        content';
        $sxe = simplexml_load_string($data);
        var_dump($sxe);
        ?>

In phpinfo() it shows that libxml is enabled and active. Any help? Thanks in advance


回答1:


You should encode the special characters using htmlspecialchars

For example: print "Content: ".htmlspecialchars($para)."<br>";



来源:https://stackoverflow.com/questions/11527447/cannot-parse-cdata-with-simplexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!