Jump to content

Archived

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

448191

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?


[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]

Share this post


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

[url=http://php.net/manual/en/function.dom-domdocument-importnode.php]DOMDocument->importNode()[/url]

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:
[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?

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.