How to insert HTML to PHP DOMNode?

You can use

Example:

// just some setup
$dom = new DOMDocument;
$dom->loadXml('<html><body/></html>');
$body = $dom->documentElement->firstChild;

// this is the part you are looking for    
$template = $dom->createDocumentFragment();
$template->appendXML('<h1>This is <em>my</em> template</h1>');
$body->appendChild($template);

// output
echo $dom->saveXml();

Output:

<?xml version="1.0"?>
<html><body><h1>This is <em>my</em> template</h1></body></html>

If you want to import from another DOMDocument, replace the three lines with

$tpl = new DOMDocument;
$tpl->loadXml('<h1>This is <em>my</em> template</h1>');
$body->appendChild($dom->importNode($tpl->documentElement, TRUE));

Using TRUE as the second argument to importNode will do a recursive import of the node tree.


If you need to import (malformed) HTML, change loadXml to loadHTML. This will trigger the HTML parser of libxml (what ext/DOM uses internally):

libxml_use_internal_errors(true);
$tpl = new DOMDocument;
$tpl->loadHtml('<h1>This is <em>malformed</em> template</h2>');
$body->appendChild($dom->importNode($tpl->documentElement, TRUE));
libxml_use_internal_errors(false);

Note that libxml will try to correct the markup, e.g. it will change the wrong closing </h2> to </h1>.

Leave a Comment