I am working on modifying the contents of an XML file generated by some other library. I'm making some DOM modifications with PHP (5.3.10) and reinserting a replacement node.
The XML data I'm working with has "
; elements before I do the manipulation and I want to keep those elements as per http://www.w3.org/TR/REC-xml/ when I'm done with the modifications.
However I'm having problems with PHP changing the "
elements. See my example.
$temp = 'Hello "XML".';
$doc = new DOMDocument('1.0', 'utf-8');
$newelement = $doc->createElement('description', $temp);
$doc->appendChild($newelement);
echo $doc->saveXML() . PHP_EOL; // shows " instead of element
$node = $doc->getElementsByTagName('description')->item(0);
echo $node->nodeValue . PHP_EOL; // also shows "
Output
<?xml version="1.0" encoding="utf-8"?>
<description>Hello "XML".</description>
Hello "XML".
Is this a PHP error or am I doing something wrong? I hope it isn't necessary to use createEntityReference in every char location.
Similar Question: PHP XML Entity Encoding issue
EDIT: As an example to show saveXML should not be converting the "
entities just like the &
which behaves properly. This $temp string should really be output as it is initially entered with the entities during saveXML().
$temp = 'Hello "XML" &.';
$doc = new DOMDocument('1.0', 'utf-8');
$newelement = $doc->createElement('description', $temp);
$doc->appendChild($newelement);
echo $doc->saveXML() . PHP_EOL; // shows " instead of element like &
$node = $doc->getElementsByTagName('description')->item(0);
echo $node->nodeValue . PHP_EOL; // also shows " &
Output
<?xml version="1.0" encoding="utf-8"?>
<description>Hello "XML" &.</description>
Hello "XML" &.