php export xml CDATA escaped -


i trying export xml cdata tags. use following code:

$xml_product = $xml_products->addchild('product'); $xml_product->addchild('mychild', htmlentities("<![cdata[" . $mytext . "]]>")); 

the problem cdata tags < , > escaped &lt; , &gt; following:

 <mychild>&lt;![cdata[my long long long text]]&gt;</mychild> 

but need:

<mychild><![cdata[my long long long text]]></mychild>  

if use htmlentities() lots of errors tag raquo not defined etc... though there no such tags in text. htmlentities() tries parse text inside cdata , convert it, dont want either.

any ideas how fix that? thank you.

upd_1 function saves xml file:

public static function saveformattedxmlfile($simplexmlelement, $output_file) {     $dom = new domdocument('1.0', 'utf-8');     $dom->preservewhitespace = false;     $dom->formatoutput = true;     $dom->loadxml(urldecode($simplexmlelement->asxml()));     $dom->save($output_file);  } 

a short example of how add cdata section, note way skips using domdocument add cdata section in. code builds <product> element, $xml_product has new element <mychild> created in it. newnode imported domelement using dom_import_simplexml. uses domdocument createcdatasection method create appropriate bit , adds node.

$xml = new simplexmlelement('<?xml version="1.0" encoding="utf-8"?><products />');  $xml_product = $xml->addchild('product'); $newnode = $xml_product->addchild('mychild'); $mytext = "<html></html>"; $node = dom_import_simplexml($newnode); $cdata = $node->ownerdocument->createcdatasection($mytext); $node->appendchild($cdata); echo $xml->asxml(); 

this example outputs...

<?xml version="1.0" encoding="utf-8"?> <products><product><mychild><![cdata[<html></html>]]></mychild></product></products> 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -