448191 Posted August 9, 2006 Share Posted August 9, 2006 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]<?phpclass 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 https://forums.phpfreaks.com/topic/17015-domdocumentsimulate-the-working-of-innerhtml/ Share on other sites More sharing options...
448191 Posted August 9, 2006 Author Share Posted August 9, 2006 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] Link to comment https://forums.phpfreaks.com/topic/17015-domdocumentsimulate-the-working-of-innerhtml/#findComment-71822 Share on other sites More sharing options...
448191 Posted August 12, 2006 Author Share Posted August 12, 2006 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]<?phppublic 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 https://forums.phpfreaks.com/topic/17015-domdocumentsimulate-the-working-of-innerhtml/#findComment-73616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.