Painless SAX Parsing with HC and ANT
The Java platform offers a rich set of XML APIs. The oldest APIs cover basic services such as parsing; the most recent ones are more specific, such as JAX-RPC that covers remote procedure calls. As a programmer, I appreciate this diversity that lets me choose the most effective API for a given task.
Power API: SAX
In my projects, I often use SAX, the oldest and the most basic XML API. SAX, which stands for Simple API for XML, offers almost unmediated access to the XML parser. If you are not familiar with SAX, I encourage you to see my article on SAX for more details. The remainder of this article assumes that you have some notions of SAX parsing.
Because it is a very thin layer on top of an XML parser, SAX packs a lot of power. On the flip side, it requires more work than alternative APIs. Specifically, the application has to track how far the parser has gone in the document with a stack or by maintaining a state as an integer.
The state tracking code is tedious to write and even more tedious to maintain. In this article, I introduce HC, the Handler Compiler, a toolkit I developed to restore the fun in SAX parsing.
HC achieves this by automatically generating the state tracking code. It gives you the best of both worlds: access to the fast, powerful SAX API without many of the headaches.
HC is distributed through an open source license on the http://www.ananas.org/hc Web site. In the remainder of this article, I will walk you through an example of HC programming.