Basically I have a script that updates an xml document in various places… However, I need the text to be in CDATA… So I attempted this:
$PrintQuestion->content->multichoice->feedback->hint->Passage->Paragraph->addChild('TextFragment', '<![CDATA[' . $value[0] . ']]>');
Unfortunately when I save the XML back to file, the < and > in cdata come up as their respective < and $gt; codes is there a way to avoid this?
Note: Our parser doesn’t know how to read the < and > codes, so this is a serious issue
after doing a print_r of my simple_xml object, the < appears as itself in the source code!
It must be the domsave that is converting it into the entity code… any ideas how to disable this?
//Convert SimpleXML element to DOM and save
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = false;
$dom->loadXML($xml->asXML());
$dom->save($filename);
Like I said in the comments, SimpleXML is very limited in the control it gives you over the DOM nodes. Here is an example on how to replace a DOMText node with a DOMCDATASection node.
For a lengthy example on how to use DOM see my answer here and some more here