Node Iterator

import org.w3c.dom.*;


// Depth first search of DOM Tree
public abstract class NodeIterator {

  // note use of recursion
  public void followNode(Node node) {
    
    processNode(node);
    if (node.hasChildNodes()) {
      NodeList children = node.getChildNodes();
      for (int i = 0; i < children.getLength(); i++) {
        followNode(children.item(i));
      } 
    }
    
  }
  
  // Override this method to do something as each node is visited
  protected abstract void processNode(Node node);
    // I could make processNode() a separate method in 
    // a NodeProcessor interface, and make followNode static
    // but I wanted to keep this example simple.

}

Previous | Next | Top | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified March 15, 2000