<?php
function output_node($node, $level=0) {
  switch($node->type) {
    case XML_ELEMENT_NODE:
          for($i=0; $i<$level; $i++)
            echo "  ";
            $node->parent();
          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;
          break;
    case XML_ENTITY_REF_NODE:
          echo $node->content;
          break;
    case XML_COMMENT_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' standalone='yes'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
[ <!ENTITY sp \"spanish\">
]>
<chapter language='en'>
    <title language='en'>Title</title>
    <tgroup cols='3'>
        <tbody>
           <row>
            <entry>a2</entry>
            <entry>c2</entry>
        </row>
        </tbody>
    </tgroup>
</chapter> ";

/* The following code traverses the xml tree node by node
   by using the methods of the xmlnode object
*/
echo "Test 1: accessing single nodes from php\n";
if(!$dom = xmldoc($xmlstr)) {
  echo "Error while parsing the document\n";
  exit;
}
echo "XML Version: ".$dom->version."\n";
echo "Standalone: ".$dom->standalone."\n";
$rootnode = $dom->root();
output_node($rootnode);

?>
