[b]phpdom.php[/b]
[code]<?php

/**
 * Brings the DOM, as you use it with JavaScript to PHP
 *
 * @package phpDOM
 * @subpackage phpDOM
 * @version 1.1
 * @license GNU General Public License
 * @copyright Copyright (c) Stas Verberkt (Legolas), 2001 - 2006
 * @link http://www.legolasweb.nl/
 */

/**
 * phpDOM class
 *
 * @package phpDOM
 * @subpackage phpDOM
 */
class phpDOM {
	/**
	 * @var string The XHTML doctype
	 */
	var $xhtmldtd = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
	/**
	 * @var string The XML doctype
	 */
	var $xmldtd = '<?xml version="1.0" encoding="utf-8" ?>';
	/**
	 * @var object The root element (html)
	 */
	var $root = false;
	/**
	 * @var bool True for a xhtml page
	 */
	var $xhtml = true;

	/**
	 * Constructor
	 *
	 * @param bool Whether the page is xhtml
	 */
	function phpDOM($xhtml = true) {
		if ($xhtml) {
			$this->root =& new Element('html');
			$this->root->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
		}
		else {
			$this->xhtml = false;
		}
	}

	/**
	 * Set the root element
	 *
	 * @param object the root object
	 */
	function setRoot(&$root) {
		if (is_a($root, 'Element') || is_a($root, 'TextNode')) {
			$this->root =& $root;
		}
		else {
			$root =& $this->root;
		}
	}

	/**
	 * Displays or returns the page
	 *
	 * @param bool Whether to return the page
	 * @return string The page (if returned)
	 */
	function display($return = false) {
		$page = $this->_buildPage();

		if ($return) {
			return $page;
		}
		else {
			echo $page;
		}
	}

	/**
	 * Builds the page
	 *
	 * @return string The page
	 * @access private
	 */
	function _buildPage() {
		$page = null;
		$page .= $this->xmldtd;
		if ($this->xhtml) {
			$page .= $this->xhtmldtd;
		}
		$page .= $this->root->display(true);
		return $page;
	}
}

/**
 * Element class
 *
 * @package phpDOM
 * @subpackage phpDOM
 */
class Element {
	/**
	 * @var array The children
	 */
	var $children = array();
	/**
	 * @var array The attributes
	 */
	var $attributes = array();
	/**
	 * @var string The type
	 */
	var $type = false;
	/**
	 * @var object Parent node
	 */
	var $parentnode = false;

	/**
	 * Constructor
	 *
	 * @param string The type of the element
	 */
	function Element($type) {
		$this->type = $type;
	}

	/**
	 * Sets an attribute
	 *
	 * @param string The attribute
	 * @param string The value
	 */
	function setAttribute($attribute, $value = null) {
		$this->attributes[$attribute] = $value;
	}

	/**
	 * Appends a child
	 *
	 * @param object The child
	 */
	function appendChild(&$element) {
		if ((is_a($element, 'Element') || is_a($element, 'TextNode')) && !$element->parentnode) {
			$this->children[] =& $element;
			$element->parentnode =& $this;
		}
	}

	/**
	 * Displays or returns the element
	 *
	 * @param bool Whether to return the element
	 * @return string The element (if returned)
	 */
	function display($return = false) {
		$element = $this->_buildElement();

		if ($return) {
			return $element;
		}
		else {
			echo $element;
		}
	}

	/**
	 * Builds the element
	 *
	 * @return string The element
	 * @access private
	 */
	function _buildElement() {
		$element = null;
		$element .= '<' . $this->type;
		if (is_array($this->attributes)) {
			foreach ($this->attributes as $attribute => $value) {
				$element .= ' ' . $attribute . '="' . $value . '"';
			}
		}
		if (!is_array($this->children)) {
			$element .= ' />';
		}
		else {
			$element .= '>';
			for ($i = 0; $i < count($this->children); $i++) {
				$element .= $this->children[$i]->display(true);
			}
			$element .= '</' . $this->type . '>';
		}
		return $element;
	}
}

/**
 * TextNode class
 *
 * @package phpDOM
 * @subpackage phpDOM
 */
class TextNode {
	/**
	 * @var string The text
	 */
	var $text;
	/**
	 * @var object Parent node
	 */
	var $parentnode = false;

	/**
	 * Constructor
	 *
	 * @var string The text
	 */
	function TextNode($text = null) {
		$this->text = $text;
	}

	/**
	 * Displays or returns the element
	 *
	 * @param bool Whether to return the element
	 * @return string The element (if returned)
	 */
	function display($return = false) {
		if ($return) {
			return $this->text;
		}
		else {
			echo $this->text;
		}
	}
}

?>

[b]rephpdom.php[/b]
<?php

/**
 * Rebuild phpDOM from XML
 *
 * @package phpDOM
 * @subpackage rephpDOM
 * @version 1.1
 * @license GNU General Public License
 * @copyright Copyright (c) Stas Verberkt (Legolas), 2001 - 2006
 * @link http://www.legolasweb.nl/
 */

/**
 * Get phpDOM
 */
require_once 'phpdom.php';

/**
 * Parser class for the rebuilding
 *
 * @package phpDOM
 * @subpackage rephpDOM
 */
class rephpDOM {
	/**
	 * @var object The phpDOM element
	 */
	var $page = false;
	/**
	 * @var string The XML
	 */
	var $xml = null;
	/**
	 * @var object The actual element
	 * @access private
	 */
	var $actual = false;
	/**
	 * @var resource The XML Parser
	 * @access private
	 */
	var $xmlparser = false;

	/**
	 * Constructor
	 */
	function rephpDOM() {
	}

	/**
	 * Sets the XML
	 *
	 * @param string The XML
	 */
	function setXML($xml) {
		if ($xml) {
			$this->xml = $xml;
		}
	}

	/**
	 * Gets the XML from a file
	 *
	 * @param string The XML
	 */
	function setXMLFromFile($file) {
		if (!$file) {
			return false;
		}

		if (function_exists('file_get_contents')) {
			$xml = @file_get_contents($file);
		}
		else {
			$fh = @fopen($file, 'rb');
			if (!$fh) {
				return false;
			}

			$xml = null;
			while (!feof()) {
				$xml .= @fread($fh, 4096);
			}
			@fclose($fh);
		}

		$this->setXML($xml);
	}

	/**
	 * Parses the XML file
	 */
	function parse() {
		if (!$this->xml) {
			return false;
		}

		$this->page =& new phpDOM(false);
		$this->xmlparser = xml_parser_create();
		xml_set_object($this->xmlparser, $this);
		xml_set_element_handler($this->xmlparser, array(&$this, '_startHandler'), array(&$this, '_endHandler'));
		xml_set_character_data_handler($this->xmlparser, array(&$this, '_cdataHandler'));
		xml_parser_set_option($this->xmlparser, XML_OPTION_CASE_FOLDING, false);
		xml_parse($this->xmlparser, $this->xml);
		xml_parser_free($this->xmlparser);
	}

	/**
	 * Handles a start element
	 *
	 * @param resource The xml parser resource
	 * @param string The name of the element
	 * @param array The attributes
	 * @access private
	 */
	function _startHandler($xp, $name, $attribs) {
		$element =& new Element($name);

		if (!$this->actual) {
			$this->page->setRoot($element);
		}
		else {
			$this->actual->appendChild($element);
		}

		foreach ($attribs as $attribute => $value) {
			$element->setAttribute($attribute, $value);
		}

		$this->actual =& $element;
	}

	/**
	 * Handles an end element
	 *
	 * @param resource The xml parser resource
	 * @param string The name of the element
	 * @access private
	 */
	function _endHandler($xp, $name) {
		if ($name == $this->actual->type) {
			$this->actual =& $this->actual->parentnode;
		}
	}

	/**
	 * Handles character data
	 *
	 * @param resource The xml parser resource
	 * @param string The character data
	 * @access private
	 */
	function _cdataHandler($xp, $cdata) {
		if ($this->actual) {
			$text =& new Textnode($cdata);
			$this->actual->appendChild($text);
		}
	}
}

?>[/code]