Here you can find the source of getElementByFilter(Document doc, NodeFilter filter)
public static List<Element> getElementByFilter(Document doc, NodeFilter filter) throws DOMException
//package com.java2s; import java.util.ArrayList; import java.util.List; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.traversal.DocumentTraversal; import org.w3c.dom.traversal.NodeFilter; import org.w3c.dom.traversal.NodeIterator; public class Main { public static List<Element> getElementByFilter(Document doc, NodeFilter filter) throws DOMException { List<Element> result = new ArrayList<Element>(); DocumentTraversal traversable = (DocumentTraversal) doc; int whatToShow = NodeFilter.SHOW_ELEMENT; NodeIterator iterator = traversable.createNodeIterator(doc, whatToShow, filter, true); Node current = null;/*from w w w . ja v a 2 s . co m*/ while ((current = iterator.nextNode()) != null) { result.add((Element) current); } return result; } }