_dom = $impl->createDocument('', ''); else { $dtd = $impl->createDocumentType($name, $public, $system); $this->_dom = $impl->createDocument('', '', $dtd); } $this->elementClass(); $this->_stack = array($this->_dom); } /* Node construction */ public function comment($data) { return $this->newComment($data, $this->_stack[0]); } public function element($name) { return $this->newElement($name, $this->_stack[0]); } public function text($content) { return $this->newTextNode($content, $this->_stack[0]); } public function fragment($xml) { return $this->newDocumentFragment($xml, $this->_stack[0]); } public function push($node) { array_unshift($this->_stack, $node); } public function pop($node = null) { $count = count($this->_stack); if ($node === null) $count--; else { for ($i = 0; $i < $count; $i++) { if ($this->_stack[$i] === $node) { $count -= $i + 1; break; } } } while (count($this->_stack) > $count) array_shift($this->_stack); } public function top() { return $this->_stack[0]; } /* Node constructors */ public function elementClass($class = 'IMuDocumentElement') { /* Prior to PHP version 5.2.2, a previously registered class had ** to be unregistered before being able to register a new class ** extending the same base class. ** ** See http://www.php.net/manual/en/domdocument.registernodeclass.php */ $this->_dom->registerNodeClass('DOMElement', null); $this->_dom->registerNodeClass('DOMElement', $class); } public function newComment($data, $parent) { $node = $this->_dom->createComment($data); $parent->appendChild($node); return $node; } public function newElement($name, $parent) { $node = $this->createElement($name); $parent->appendChild($node); return $node; } public function newTextNode($content, $parent) { $node = $this->_dom->createTextNode($content); $parent->appendChild($node); return $node; } public function newDocumentFragment($xml, $parent) { $node = $this->_dom->createDocumentFragment(); $node->appendXML($xml); $parent->appendChild($node); } /* DOM wrapper */ public function getDOM() { return $this->_dom; } /* properties */ public function __get($name) { return $this->_dom->$name; } public function __set($name, $value) { $this->_dom->$name = $value; } /* methods */ public function createElement($name, $value = '') { $node = $this->_dom->createElement($name, $value); $node->document = $this; return $node; } public function __call($name, $args) { return call_user_func_array(array($this->_dom, $name), $args); } protected $_dom; protected $_stack; } class IMuDocumentElement extends DOMElement { public $document; public function attr($name, $value = null) { if ($value !== null) $this->setAttribute($name, $value); return $this->getAttribute($name); } public function comment($data) { return $this->document->newComment($data, $this); } public function element($name) { return $this->document->newElement($name, $this); } public function text($content) { return $this->document->newTextNode($content, $this); } public function fragment($xml) { return $this->document->newDocumentFragment($xml, $this); } public function push() { $this->document->push($this); } public function pop() { $this->document->pop($this); } } ?>