About Roger Hughes

Approaches to XML – Part 2 – What about SAX?

Part 1 introduced the idea that there are different ways to approach XML parsing and highlighted the point that XML is NOT A STRING; rather it’s an object oriented document model that can be represented using a string. Today’s blog continues this discussion using my Pete’s Perfect Pizza scenario. If you remember, Pete’s just popped his head around the door and asked you to enhance the system so that the front desk can send orders for multiple pizzas in a single XML message. You know that your simple string parsing code is flawed and won’t cut the mustard, so you Google 1 more on XML and come up with the idea of using SAX.

SAX, or Simple API for XML has been around for many years and, as far as I can recollect, was originally a development lead by David Megginson before the turn of the millennium. In those days, you had to download the Java version of SAX from David’s personal web site. This developed into the SAX Project before finally being added to Java Standard Edition 1.4.

SAX is a streaming interface for XML, which means that applications using SAX receive event notifications about the XML document being processed an element, and attribute, at a time in sequential order starting at the top of the document, and ending with the closing of the ROOT element. This means that it’s extremely efficient at processing XML in linear time without placing too many demands upon system memory.

Back to Pete’s, you work hard and come up with the following SAX parser based class:

This blog isn’t here to demonstrate how to use SAX, there are lots of the examples available if you look around, but lets take a critical look at the code and the first thing to notice is that the order(...) method now takes an input stream rather than a string as befitting a stream based API:

public List<PizzaOrder> order(InputStream xml)

The next thing to note is that the PizzaParser uses a nested class, PizzaContentHandler that extends the SAX helper class DefaultHandler. The PizzaContentHandler class captures a list of PizzaOrder beans and passes them back to the enclosing class for return to the caller. This means that all you need to do to get hold of the SAX events is to override handler methods such as startElement(...), endElement(...) etc. If you take a closer look a the code, you’ll realise that it’s pretty complex. All it has to do is to create an output list, yet there are multiple if() statements, temporary arrays and boolean switches that are used to grab hold of the right bit of information from the right point in the document. This is downside to SAX: it’s complexity places more of burden on the programmer and makes your code more error prone.

It is, however, more resilient than the previous string based attempt as the unit tests below demonstrate:

These tests demonstrate the scenarios of processing XML messages with and without white-space (fixing yesterday’s problem) together with a message that includes an order for multiple pizzas.

It’s all working really well, but Pete’s big ideas are coming fruition. He’s now expanding into a world wide concern with multiple kitchens around the world and an online presence. Pete hires some rinky-dinky business consultants who create a new pizza order XML schema and combine it with their existing customer schema. This is dropped into your email inbox and you wonder what to do next…

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Recent Jobs

No job listings found.

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.