Sunday, 8 February 2015

PHP How to use quot; entities in XML with DOMdocument



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://ift.tt/RiStca 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


No comments:

Post a Comment