The joy of SAX

I was perusing the source generated from a psuedo-‘MDA’ tool that is in heavy use at my new workplace. For every domain class the tool generates a dumb structure class or ‘bean’ with setters and getters, plus numerous translators to/from DOM/XML Strings/various other formats. I noticed the translator to XML String was an extension of the DOM translator – basically the code would walk through the domain object and build a DOM equivalent in memory, then call an XSL transform on the DOM to output XML in String form. Yikes!

So I ripped some of the generated code out into a scratch project and made the same traversal of the object model generate events sent to a SAX ContentHandler. I then wired this to the Xerces XMLSerializer which outputs formatted XML text to a Writer – and wrote a test that it output the same text as the existing transformer.

I expected to get something like 3-4x performance improvement in the translator, so I got a little surprise when my next test showed me this result:

Old method: 10000 cycles in 42578ms.

New method: 10000 cycles in 2110ms.

Wow! I really like SAX – in my last project we built some serious functionality around SAX processing of very large XML documents, performing various filtering validation and transformation on the SAX events without ever loading an entire graph into memory.

Aside: I picked up XStream again the other day, definitely falls in the very nifty basket…

Leave a Reply

Your email address will not be published. Required fields are marked *