An XML Pull Parser

The SAX API for parsing XML is called a "push-parsing” API because the SAX parser “pushes” tokens or events to your code. It is also possible to parse XML using the reverse architecture, in which your code “pulls” tokens from the XML parser as needed. The TAX class of Example 19-6 does just that. TAX is an acronym for Trivial API for XML. It is a pull parser (actually more of a fancy tokenizer) for a subset of XML. Despite its simplicity, it is useful for a variety of XML parsing tasks. The TAX.Parser class relies on the Tokenizer interface (defined in Example 2-8) and on the various implementations of that interface (see Chapter 2, Chapter 3, and Chapter 6). The TAX class is simply a holder for inner classes and token type constants. In addition to the TAX.Parser class, TAX also holds a Token class, a TokenType class (an enumerated type), and a ParseException class.

Before diving into the details of the TAX parser, it is probably easiest to first study how the parser is typically used. Example 19-5 is a program much like the ListServlets program of Example 19-1: it parses a web.xml file and outputs the servlet name-to-class mappings and name-to-URL mappings defined in the file.

Example 19-5. ListServlets2.java

package je3.xml; import java.io.*; /** * Parse a web.xml file using the TAX pull parser and print out the servlet * name-to-class and name-to-url mappings. **/ public class ListServlets2 { public static void main(String[ ] args) throws IOException, TAX.ParseException ...

Get Java Examples in a Nutshell, 3rd Edition 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.