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 you're interested in.

Here's an example named ch11_04.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 ch11_01.xml:

%java ch11_04 ch11_01.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 pass the document to search and the element name to search for to the contructor of this new class:

 import javax.xml.parsers.*; import org.w3c.dom.*; public ...

Get Real World XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.