title: PHP How to use quot; entities in XML with DOMdocument
body: <p>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.</p>
  
  <p>The XML data I'm working with has <code>&amp;quot</code>; elements before I do the manipulation and I want to keep those elements as per <a href="http://www.w3.org/TR/REC-xml/" rel="nofollow noreferrer">http://www.w3.org/TR/REC-xml/</a> when I'm done with the modifications.</p>
  
  <p>However I'm having problems with PHP changing the <code>&amp;quot;</code> elements.  See my example.</p>
  
  <pre><code>$temp = 'Hello &amp;quot;XML&amp;quot;.';
  $doc = new DOMDocument('1.0', 'utf-8');
  $newelement = $doc-&gt;createElement('description', $temp);
  $doc-&gt;appendChild($newelement);
  echo $doc-&gt;saveXML() . PHP_EOL; // shows " instead of element
  $node = $doc-&gt;getElementsByTagName('description')-&gt;item(0);
  echo $node-&gt;nodeValue . PHP_EOL; // also shows "
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; 
  &lt;description&gt;Hello "XML".&lt;/description&gt;
  
  Hello "XML".
  </code></pre>
  
  <p>Is this a PHP error or am I doing something wrong?  I hope it isn't necessary to use createEntityReference in every char location.</p>
  
  <p>Similar Question:
  <a href="https://stackoverflow.com/questions/9279195/php-xml-entity-encoding-issue">PHP XML Entity Encoding issue</a></p>
  
  <hr>
  
  <p>EDIT: As an example to show saveXML should not be converting the <code>&amp;quot;</code> entities just like the <code>&amp;amp;</code> which behaves properly.  This $temp string should really be output as it is initially entered with the entities during saveXML().</p>
  
  <pre><code>$temp = 'Hello &amp;quot;XML&amp;quot; &amp;amp;.';
  $doc = new DOMDocument('1.0', 'utf-8');
  $newelement = $doc-&gt;createElement('description', $temp);
  $doc-&gt;appendChild($newelement);
  echo $doc-&gt;saveXML() . PHP_EOL; // shows " instead of element like &amp;amp;
  $node = $doc-&gt;getElementsByTagName('description')-&gt;item(0);
  echo $node-&gt;nodeValue . PHP_EOL; // also shows " &amp;
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;description&gt;Hello "XML" &amp;amp;.&lt;/description&gt;
  
  Hello "XML" &amp;.
  </code></pre>