Filtering XML Documents

The previous example displayed the entire document, but as we saw in the previous chapter, 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 a new example named ch12_04.java. In this case, I'll let the user specify what document to search and what element name to search for. This will display all <ITEM> elements in ch12_01.xml:

%java ch12_04 ch12_01.xml ITEM 

This program is not difficult to write, now that we've written the indenting parser example. Note, however, that we have to handle not just the specific element that the user is searching for, but also the element's children that are contained inside the ...

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.