I found DOMDocument very useful for extracting raw XML from a field in a MySQL database.
The following always gave me character entities for the XML tag brackets (< >) in the output (whether I used the utf8_encode function or htmlspecialchars function or neither of them on $row['text']):
<?php
$next_elem = $doc->createElement( $row['node_type'], utf8_encode($row['text']) );
$section_elem->appendChild($next_elem);
?>
I changed the code to use a DOMDocumentFragment and now the XML I stored in my database comes out as XML in my output with proper tag brackets instead of html character entities:
<?php
$next_elem = $doc->createDocumentFragment();
$next_elem->appendXML($row['text']);
$section_elem->appendChild($next_elem);
?>