« Last Day at webMethods | Main | Attrition at the Alma Mater »
August 02, 2005
SAX Parsing
Today at work I had to write some code using the SAX (Simple API for XML) interfaces to hydrate some Java objects. I have worked with XML quite a bit over the past few years, but still had not done much with SAX. I still prefer using a pull parser or a Java-friendly DOM interface, but using SAX was at least mildly interesting (it doesn't take much to amuse me these days).
SAX is a "push" parser that acts like a fire hose spewing out interesting events as it traverses an XML tree. The ContentHandler interface is where all the the action takes place as it receives the events that provide access to the data. When using SAX to initialize objects you essentially need a ContentHandler that understands how the content model relates with the object model. Unless you hard-wire your XML elements to match the fields within your Java object, encode some metadata in the XML, and use reflection, this means that you'll have a ContentHandler for each different chunk of XML that needs to be processed.
One of the nice things about the experience was being able to pass control between different handlers during the parsing process. This allowed for handlers to be easily reused in different contexts assuming the XML within those contexts is the same.
An important aspect of using SAX is that the content handler typically needs to maintain some level of state during the parsing process to know when to peel off the interesting bits. This is one things that I found lacking in the cursory examples on the web, most of which simply spewed some data to System.out. I would consider posting a good SAX example that actually does something meaningful, but the truth is that SAX parsing in Java is so 1999 that I probably won't bother... I just happen to arrive late to this particular party.
Posted by harris at August 2, 2005 10:25 PM
Trackback Pings
TrackBack URL for this entry:
http://harrisreynolds.net/mt/mt-tb.cgi/3