PHP DOM grafting
I wanted to be able to import a node from a text file into a DOM object in PHP.
Below are the relevant portions of my extended DOM class:
class fancyDOM extends DOMDocument() { public function fancyDOM($template) { $this->loadHTMLFile($template); } function importTemplate($target, $file, $root = 'div') { $temp = new fancyDOM($file); $el = $this->importNode($temp->getElementsByTagName($root)->item(0), true); $this->getElementsById($target)->appendChild($el); } }
The file it's loading contains arbitrary HTML contained in some sort of container, div being the default.
<div> <table> <tr> <td>test 1</td> <td>test 2</td> </tr> </table> </div>
You can now import that file into an existing DOM
$html = new fancyDOM('main.html'); $html->importTemplate('content-container', 'main-menu.html');











