Jump to content


Photo

DOMDocument::Simulate the working of innerHTML


  • Please log in to reply
2 replies to this topic

#1 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 09 August 2006 - 01:44 PM

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?


<?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);	
	}
?>

<?php
$body = $page->body(); //Returns the 'body' element node of $page->domDoc.
$page->appendChunk($body,file_get_contents('indexHTMLFrame.html'));
?>


#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 09 August 2006 - 02:11 PM

Nevermind, I think I've found what I was looking for.

DOMDocument->importNode()

#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 12 August 2006 - 08:43 AM

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:
<?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);
}
?>

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users