9.4 Node Collections

<..DOM extended tree traverser..>
 class TreeTraverser{
     static String indent="";
   TreeTraverser (Node node){
     String s = node.getNodeValue();
     System.out.println( indent + node.getNodeName()
                 + " <<" + (s==null? "null" : s.trim()) + ">>");
     int i;
     indent += "  ";
     if( node.hasAttributes() ){
        System.out.println( indent + "ATTRIBUTES:" );
        NamedNodeMap attributes = node.getAttributes()   ;
        for( i=0; i < attributes.getLength(); i++ ){
           new TreeTraverser ( attributes.item(i) );
     } }
     if( node.hasChildNodes() ){
        System.out.println( indent + "CHILDREN:" );
        NodeList children = node.getChildNodes();
        for( i=0; i < children.getLength(); i++ ){
           new TreeTraverser ( children.item(i) );
     } }
     indent = indent.substring(2);
 } }
-_-_-
<?xml version="1.0" encoding="UTF-8"?>  
<root xmlns="x:y" attr="ignored">  
  <section>  
    A &amp; B  
  </section>  
</root>  
#document <<null>>  
  CHILDREN:  
  root <<null>>  
    ATTRIBUTES:  
    xmlns <<x:y>>  
    attr <<ignored>>  
    CHILDREN:  
    #text <<>>  
    section <<null>>  
      CHILDREN:  
      #text <<A & B>>  
    #text <<>>

Note. The attributes are not listed!