How to prevent XMLWriter or DOMDocument from decoding HTML tags

I am writing a script that pulls XHTML data from a database and puts it into an XML document. However, whenever I assign the value of the XHTML string to an element, it ends up getting converted to the decoded version (showing ampersands rather than less than/greater than tags in the source).

I’ve tried this with both XMLWriter and DOMDocument, with exactly the same result. Please let me know if you have any suggestions!

Here is some code to demonstrate the issue:

XMLWriter version:

$sampleHTML = "Testing
";
$w=new XMLWriter();
$w->openMemory();
$w->startDocument(‘1.0’,‘UTF-8’);
$w->setIndent(4);
$w->startElement(“testHTML”);
$w->writeElement( ‘code’ , $sampleHTML );
$w->endElement();
$w->endDocument();
echo $w->outputMemory(true);

DOMDocument version:

$sampleHTML = "Testing
";
$domOutputMessage = new DOMDocument(‘1.0’, ‘utf-8’);
$root = $domOutputMessage->createElement(‘testHTML’);
$domOutputMessage->appendChild($root);
$nodeCode = $domOutputMessage->createElement(‘code’, $sampleHTML);
$root->appendChild($nodeCode);
echo $domOutputMessage->saveXML();

The output of both of these is:

<?xml version="1.0" encoding="UTF-8"?> Testing <BR/> <img src=''/>
Sponsor our Newsletter | Privacy Policy | Terms of Service