DOM Code Example

import org.apache.xerces.parsers.DOMParser;

public class DOMWriter {

  public static void main( String[] args) { 
    try {
      DOMParser parser = new DOMParser();
      
      parser.parse( "test.xml"); 
      
      Document document = parser.getDocument();
      processElement( document.getDocumentElement());
    
    } catch (SAXException e) {
      e.printStacktrace();
    } catch (IOException e) {
      e.printStacktrace();
    }
  }
  
  private static void processElement( Element element) {
    NodeSet nodes = element.getChildNodes();
    
    System.out.println( "<"+element.getNodeName()+">");
    
    for ( int i = 0; i < nodes.getLength(); i++) {
      Node node = nodes.item( i);
      
      if ( node.getNodeType() == Node.ELEMENT_NODE) {
        processElement( (Element)node);
      } else {
        System.out.println( node.getNodeValue());
      }
    }
    
    System.out.println( "</"+element.getNodeName()+">");
  }
}