<?php

function output_node($node, $level=0) {
  switch($node->type) {
    case XML_ELEMENT_NODE:
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo "<".$node->name;
      $attributes = $node->attributes();
      if(is_array($attributes))
        for(reset($attributes); $attrname = key($attributes); next($attributes))
          echo " ".$attrname."=".$node->getattr($attrname);
      echo ">\n";
      $children = $node->children();
      for($i=0; $i < count($children); $i++)
        output_node($children[$i], $level+1);
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo "</".$node->name.">\n";
      break;
    case XML_TEXT_NODE:
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo $node->content;
      echo "\n";
      break;
  }
}

function list_attr($node) {
  $attr = domxml_attributes($node);
  for(reset($attr); $key = key($attr); next($attr)) {
    echo $key."=".$attr[$key]."\n";
  }
}

$xmlstr = "<?xml version='1.0'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'>
<chapter language='en'><title language='en'>Title</title>
<para language='en'>
<informaltable language='en'>
<tgroup cols='3'>
<tbody>
<row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row>
<row><entry>a2</entry><entry>c2</entry></row>
<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
</tbody>
</tgroup>
</informaltable>
</para>
</chapter> ";

$dom = dom($xmlstr);
echo "XML Version: ".$dom->version."\n";
$dtd = $dom->dtd();
$rootnode = $dom->root();
/* The following line causes a segm fault if more than 5 methods are in
   class node, see ext/domxml/domxml.c */
$attr = $rootnode->getattr("language");

/* if the above weren't there, php will probably crash somewhere after here */
output_node($rootnode);

$doc = newxmldoc("1.0");
$root = $doc->addroot("HTML");
echo $root->name;
$root->newchild("TITLE", "Hier der Titel");
$root->newchild("BODY", "");
echo $doc->dumpmem();

?>
