name = $name; $this->value = $value; } } class XMLNode { var $name; var $attributes = array(); var $content; var $children; var $parent = null; function &XMLNode($name, $attributes) { $this->name = $name; foreach ($attributes as $name=>$value) { $this->attributes[$name] = new XMLAttribute($name, $value); } $this->children = array(); } function setContent(&$data) { $this->content = $data; } function &addChild(&$child) { $child->setParent($this); $this->children[] =& $child; return $this->children[count($this->children) - 1]; } function setParent(&$parent) { $this->parent =& $parent; } } class XMLParser { var $parser; var $root; var $last = null; function XMLParser() { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, 'start', 'end'); xml_set_character_data_handler($this->parser, 'cdata'); } function parse($xml) { xml_parse($this->parser, $xml); } function start($parser, $name, $attrs) { $tag = trim($name); $node = new XMLNode($tag, $attrs); if (is_null($this->last)) { $this->root =& $node; } else { $this->last->addChild(&$node); } $this->last =& $node; } function end($parser, $name) { $this->last =& $this->last->parent; } function cdata($parser, $data) { if (!is_null($this->last)) $this->last->setContent(trim($data)); } function &getRoot() { return $this->root; } } class Transformer { var $root; function Transformer($root) { $this->root = $root; } function toHTML() { return $this->_toTable($this->root); } function _toTable($node) { $out = "
| ".$node->name." | \n|
|---|---|
| Attributes | \n". "\n";
if (empty($node->attributes)) {
$out .= " \n";
} else {
foreach ($node->attributes as $attr) {
$out .= "{$attr->name} = {$attr->value} \n"; } } $out .= " | \n
| Content: | \n". ""; $out .= (trim($node->content)) ? $node->content : ' '; $out .= " | \n
| ChildNodes: | \n". "\n"; foreach ($node->children as $child) { $out .= $this->_toTable($child)."\n"; } $out .= " | \n