Having spent a few hours researching I have been unable to come up with an answer for this. I am trying to send an XML string to a third party so I need to encode some character
You need to use a createEntityReference function to get it to pass through unmolested.
Example:
$XMLDoc = new DOMDocument('1.0', 'utf-8');
$comments = $XMLDoc->createElement('Comments');
$text1 = $XMLDoc->createTextNode('An apostrophe here < ');
$text2 = $XMLDoc->createEntityReference('apos');
$text3 = $XMLDoc->createTextNode(' > Pound sign: ');
$text4 = $XMLDoc->createEntityReference('pound');
$comments->appendChild($text1);
$comments->appendChild($text2);
$comments->appendChild($text3);
$comments->appendChild($text4);
$XMLDoc->appendChild($comments);
echo $XMLDoc->saveXML();
Note that some items are encoded by default by createTextNode. < and > are encoded, pound signs are not. Hope this helps!