Filtering XML Documents

The previous example displayed the entire document, but you can be more selective than that through a process called filtering. When you filter a document, you extract only those elements that you're interested in.

Here's an example named searcher.java. In this case, I'll let the user specify what document to search and what element name to search for like this, which will display all <ITEM> elements in customer.xml:

%java searcher customer.xml ITEM

I'll start this program by creating a new class, FindElements, to make the programming a little easier. All I have to do is to pass the document to search and the element name to search for to the constructor of this new class:

 import org.w3c.dom.*; import org.apache.xerces.parsers.DOMParser; ...

Get Inside XML now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.