I have an xml file with elements in the form of
<TEST id="messageId"><![CDATA[Text I want to manipulate]]></TEST> I can access the text within the CDATA with the code below
$dom = new DOMDocument; $dom->Load('/path/to/file.xml'); foreach ($dom->getElementsByTagName('TEST') as $element) { $value = $element->nodeValue; //Text I want to manipulate // do stuff to $value $element->nodeValue = $value; // issue } $dom->save('/path/to/different/file.xml'); However when the xml file is saved, the CDATA is missing and I get
<TEST id="messageId">Manipulated text</TEST> I have read I need to use createCDATASection() but I cannot quite figure out how to use it in this context. If I replace $element->nodeValue = $value with $dom->createCDATASection($value) then I just get the original unmodified XML file saved.
I want to get back to the original format but with the manipulated text
<TEST id="messageId"><![CDATA[Manipulated text]]></TEST>