Jump to content

DOMDocument::Simulate the working of innerHTML


448191

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?


[code]<?php
class DOMPage
{
public $domDoc;
public $docElement;
public $head;
public $title;

function __construct()
{
$this->domDoc = new DOMDocument();
//root element:
$this->docElement = $this->domDoc->createElement('html');
$this->docElement->setAttribute('xmlns','http://www.w3.org/1999/xhtml');
$this->domDoc->appendChild($this->docElement);
//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)) {
trigger_error('Argh..',E_USER_ERROR);
}
echo $chunkDoc->documentElement->firstChild->firstChild->nodeName;
$parentNode->appendChild($chunkDoc->documentElement->firstChild->firstChild);
}
?>[/code]

[code]<?php
$body = $page->body(); //Returns the 'body' element node of $page->domDoc.
$page->appendChunk($body,file_get_contents('indexHTMLFrame.html'));
?>[/code]
Link to comment
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:
[code]<?php
public function appendChunk($parentNode, $chunk)
{
$frag = $this->domDoc->createDocumentFragment();
$publ = $this->domDoc->createElement('public');
$publ->setAttribute('publicId',$this->publicId);
$publ->setAttribute('uri','file:'.$this->systemId);
$frag->appendChild($publ);
if(!$frag->appendXML($chunk)) {
throw new Exception('Appending chunk failed.');
}
$parentNode->appendChild($frag);
}
?>[/code]

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.