I've seen a lot of questions with good answers about how to load a XML string with CDATA, or how to write an element with CDATA content, but I haven't been able to find a way to edit a XML that already contains CDATA and keep those previous CDATA after using $xml->asXML()
.
Is there a way to edit a XML that already contains CDATA and keep those previous CDATA after using $xml->asXML()
?
Look at this scenario:
<?php
$str_xml = "<myxml><mytag><![CDATA[In this content, 8 > 2 & 1 < 9, and 10 is a 10% of 100. ]]></mytag></myxml>";
try {
echo "XML with CDATA:
";
$xml = simplexml_load_string($str_xml, null, LIBXML_NOCDATA);
echo $xml->asXML() ."
";
} catch(Exception $e){
echo "XML with CDATA:
";
echo $e->getMessage() ."
";
}
?>
After running this sample, the output is:
<?xml version="1.0"?>
<myxml><mytag>In this content, 8 > 2 & 1 < 9, and 10 is a 10% of 100. </mytag></myxml>
As you can see, this output XML string has not CDATA tag values anymore. It will not fail to load another simplexml_load_string
with this output, but the existing CDATA tags are gone, and also the special characters are replaced by their corresponding html entities.
I need to know if there is a way to read any XML containing any number of CDATA node values, and that after using $xml->asXML()
, the output string still contains the previous CDATA tag values.
Thank you for your help.