Traversing a DOM tree using a NodeIterator

(DOM Level 2!) A NodeIterator can come in pretty handy to walk over all the nodes, even though you loose the structure of the document. See the questions/answers about TreeWalker to retain the structure.

The following simple example shows you how it works. If you have played around with FileFilters before, it’s a no-brainer. It shows all the zip codes that contain a “8″.

For more information on the NodeIterator, the NodeFilter and their options, check out the API docs: NodeIterator, NodeFilter

customers.xml (!!remove the space between ? and xml):

<? xml version="1.0" encoding="UTF-8"?>
<customers>
   <customer id="C12345">
      <name>Joris Van den Bogaert</name>
      <address>
         <addressline>Handelskaai 3</addressline>
         <zip>1000</zip>
         <location>Brussels</location>
         <country>BELGIUM</country>
      </address>
   </customer>
   <customer id="C23495">
      <name>John Doe</name>
      <address>
         <addressline>5, S 5th Ave.</addressline>
         <zip>59715</zip>
         <location>Bozeman, MT</location>
         <country>US</country>
      </address>
   </customer>
   <customer id="C03429">
      <name>John Babcock</name>
      <address>
         <addressline>73, Broad street</addressline>
         <zip>06418</zip>
         <location>Chester, CT</location>
         <country>US</country>
      </address>
   </customer>
   <customer id="C12345">
      <name>Dominique Bodard</name>
      <address>
         <addressline>21-23, Rue de Madrid</addressline>
         <zip>75008</zip>
         <location>Paris</location>
         <country>FRANCE</country>
      </address>
   </customer>
</customers>

Main.java:

import org.w3c.dom.*;
import org.w3c.dom.traversal.*;
  
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
 
import java.io.*;
   
public class Main
{
   public static void main(String []args) {
      Document doc;
  
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         doc = db.parse(new File("customers.xml")); 
 
         if (!doc.isSupported("Traversal", "2.0")) {
            System.out.println("Traversal not supported in your parser version");
            System.exit(1);
         }
 
         DocumentTraversal dt = (DocumentTraversal) doc;
         NodeIterator iterator = dt.createNodeIterator(doc, 
                                                       NodeFilter.SHOW_ALL, 
                                                       new ZipFilter(), 
                                                       true);
         Node node;
         while ((node = iterator.nextNode()) != null) {
            System.out.println(node.getFirstChild().getNodeValue());
         }
      }
      catch(Exception e) {
         e.printStackTrace();
      }
   } 
}
 
class ZipFilter implements NodeFilter
{
   public short acceptNode(Node n) {
      if (n.getNodeName().equals("zip") && n.getFirstChild().getNodeValue().indexOf("8") > -1) { 
         return FILTER_ACCEPT;
      }
      return FILTER_SKIP;
   }
}

outputs:

06418
75008