Except bit-masks identifying the node-types to shows during traverse the DOM-tree, both iterator and walker allows to use user-defined node filters. Such a filters can performs more complex actions in order to decide should the node be passed or not.

The sample below shows element-name based filter usage:

 import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;

import org.w3c.dom.*;

 * This sample program using tree-walker for
 * non-linear traverse of DOM-document.

public class Test {
  public static void main(String[] args) {
    try {
      // load the document from a file:
      DocumentBuilderFactory factory = 
      DocumentBuilder loader = factory.newDocumentBuilder();
      Document document = loader.parse("sample.xml");

      // get the traversal:
      DocumentTraversal traversal = (DocumentTraversal) document;
      NodeIterator iterator = traversal.createNodeIterator(
        document.getDocumentElement(), NodeFilter.SHOW_ALL,
        new ItemFilter(), true);

      for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
        System.out.println("Element: " + ((Element) n).getTagName());

    } catch (Exception ex) {
  // accepts only DOM-element with tag-name 'item':
  private static final class ItemFilter implements NodeFilter {
    // this will be applied to each specified node in order to indicate
    // should it be visible in the logical view:
    public short acceptNode(Node n) {
      if (n instanceof Element) {
        if (((Element) n).getTagName().equals("item")) {
          return NodeFilter.FILTER_ACCEPT;
      return NodeFilter.FILTER_REJECT;