Jump to content


This topic is now archived and is closed to further replies.


DOMDocument::Simulate the working of innerHTML

Recommended Posts

If been trying to create a DOMPage class, wich I plan on using as sort of an "indexed output buffer". Don't know how else to put it.

Anyway, it uses php5's DOMDocument class. Thing is, I want to be able to attach whole chunks of html to the document, sort of like innerHTML works (wich unfortunately isn't supported, nor in the recommendation).

Below is what I attempted in the form of DOMPage::appendChunk().

It won't work though, I get a 'Wrong Document Error' exception.

So my guess is it's not possible to attach one objects node to another objects node.
I was afraid of that but was hoping if the nodes where of the same document type it would work. It won't, that much is clear.

The question: Anyone got any other ideas on how to simulate the working of innerHTML?

class DOMPage
public $domDoc;
public $docElement;
public $head;
public $title;

function __construct()
$this->domDoc = new DOMDocument();
//root element:
$this->docElement = $this->domDoc->createElement('html');
//DomDocument settings:
$this->domDoc->validateOnParse = true;
$this->domDoc->preserveWhitespace = false;
$this->domDoc->formatOutput = true;
//Create pagehead element:
$this->head = $this->domDoc->createElement('head');
$this->head = $this->docElement->appendChild($this->head);
public function appendChunk($parentNode, $html)
$chunkDoc = new DOMDocument();
if(!$chunkDoc->loadHTML($html)) {
echo $chunkDoc->documentElement->firstChild->firstChild->nodeName;

$body = $page->body(); //Returns the 'body' element node of $page->domDoc.

Share this post

Link to post
Share on other sites
Nevermind, I think I've found what I was looking for.


Share this post

Link to post
Share on other sites
Hmmm. I found a method that seems more appropiate than creating a separate instance of DOMDocument, DOMDocumentFragment->appendXML(), but run into a problem with HTML enitities: they aren't defined.

This is my most recent attempt:
public function appendChunk($parentNode, $chunk)
$frag = $this->domDoc->createDocumentFragment();
$publ = $this->domDoc->createElement('public');
if(!$frag->appendXML($chunk)) {
throw new Exception('Appending chunk failed.');

I got the public tag idea from a user comment on php.net, but can't get it to work.

$this->systemId conatains the path to a local xhtml dtd.

Ideas anyone?

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.