[Slide]

Document doc= new DocumentImpl();
  Element root = doc.createElement("person");     
    Element item = doc.createElement("name");       
      item.appendChild( doc.createTextNode("Jeff") );
    root.appendChild( item );                       
    item = doc.createElement("age");                
      item.appendChild( doc.createTextNode("28") );       
    root.appendChild( item );                       
  root.appendChild( item );                       
doc.appendChild( root );                        

void traverse ( Node node )
{
   int type = node.getNodeType();
  
   if ( node.getNodeType() == Node.DOCUMENT_NODE )
   {
       elements = 0;
       attributes = 0;
       traverse(((Document)node).getDocumentElement());
   }
   else if ( node.getNodeType() == Node.ELEMENT_NODE )
   {
       elements++;
       NamedNodeMap attrs = node.getAttributes();
       if (attrs != null) {
           attributes += attrs.getLength();
       }
       NodeList children = node.getChildNodes();
       if (children != null) {
           int len = children.getLength();
           for (int i = 0; i < len; i++) {
              traverse(children.item(i));
           }
       }
   }

   // etc.
}

Copyright (c) 2001 Mary Holstege
All rights reserved.