obj = $root; } else { $doc = new DOMDocument(); $this->obj = $doc->createElement($name); } $this->normalize(); } function normalize() { $obj = $this->obj; $this->nodeType = $obj->nodeType; $this->nodeName = $obj->nodeName; $this->nodeValue = $obj->nodeValue; $firstChild = $obj->firstChild; if ($firstChild instanceof DOMText) { $this->firstChild = new _Text($firstChild, null); } elseif ($firstChild instanceof DOMCDATASection) { $this->firstChild = new _CDATASection($firstChild, null); } $this->childNodes = new _NodeList($obj->childNodes); $this->attributes = new _NamedNodeMap($obj->attributes); } function firstChild() { return $this->firstChild; } function nodeValue() { return $this->nodeValue; } function appendChild(&$node) { } function hasAttributes() { return $this->obj->hasAttributes(); } function hasChildNodes() { return $this->obj->hasChildNodes(); } } class _Document extends _Node { var $documentElement = null; private $doc; function __construct($version) { $this->doc = new DOMDocument($version); } function appendChild(&$element) { $this->doc->appendChild($element->obj); if (is_null($this->documentElement)) $this->documentElement = &$element; } function createElement($name) { return new _Element($this->doc, $name); } function createTextNode($string) { return new _Text($this->doc, $string); } function createCDATASection($string) { return new _CDATASection($this->doc, $string); } function getElementById($id) { return $this->doc->getElementById($id); } function getElementsByTagName($name) { $nodes = $this->doc->getElementsByTagName($name); return new _NodeList($nodes); } function validate() { return $this->doc->schemaValidateSource($this->doc->saveXML()); } function saveHTMLFile($filename) { return file_put_contents($filename, $this->saveHTML()); } function loadHTMLFile($filename) { $this->loadHTML(file_get_contents($filename)); } function loadHTML($string) { $this->doc->loadHTML($string); } function saveHTML() { return $this->doc->saveHTML(); } function load($filename) { $this->doc->load($filename); $this->documentElement = new _Element($this->doc->documentElement, null); } function loadXML($string) { $this->doc->loadXML($string); } function save($filename) { $this->doc->save($filename); } function saveXML() { return $this->doc->saveXML(); } } class _Element extends _Node { function __construct($doc, $name) { parent::__construct($doc, $name); } function normalize() { parent::normalize(); $this->tagName = $this->obj->tagName; $this->nodeValue = $this->getNodesValue($this->obj->childNodes); } function getNodesValue($nodes) { $value = ""; foreach ($nodes as $node) { if ($node->nodeType == XML_ELEMENT_NODE) { $value .= $this->getNodesValue($node->childNodes); } else { $value .= $node->nodeValue; } } return $value; } function getAttribute($name) { return $this->obj->getAttribute($name); } function getElementsByTagName() { } function hasAttribute($name) { return $this->obj->hasAttribute($name); } function setAttribute($name, $string) { return $this->obj->setAttribute($name, $string); } function appendChild(&$child) { $ret = $this->obj->appendChild($child->obj); $this->normalize(); return $ret; } } class _Text extends _Node { function __construct(&$root, $string) { if ($root instanceof DOMText) { parent::__construct($root, $string); } else { $this->obj = $root->ownerDocument->createTextNode($string); } $this->normalize(); } } class _CDATASection extends _Node { function __construct(&$root, $string) { if ($root instanceof DOMCDATASection) { parent::__construct($root, $string); } else { $this->obj = $root->ownerDocument->createCDATASection($string); } $this->normalize(); } } class _NodeList { var $length; function __construct($nodes) { $this->length = $nodes->length; $this->nodes = $nodes; } function item($index) { if ($index < $this->length) { $elem = $this->nodes->item($index); switch ($elem->nodeType) { case XML_ELEMENT_NODE: $elem = new _Element($elem, null); break; case XML_TEXT_NODE: $elem = new _Text($elem, null); break; case XML_CDATA_SECTION_NODE: $elem = new _CDATASection($elem, null); break; } return $elem; } return null; } } class _NamedNodeMap { var $nodes; var $length; function __construct($nodes) { $this->length = $nodes->length; $this->nodes = $nodes; } function item($index) { if ($index < $this->length) return new _Node($this->nodes->item($index), null); return null; } } class _Attr extends _Node { function __construct(&$root, $name) { parent::__construct($root, $name); } } ?>