Compare/Contrast: Java APIs for XML

If you’re looking at this blog, you’ve likely written programs dealing with data. What if your data set is so long that it starts to become impractical to hard code all of it? That’s where XML files become a strong option to store all that data in a practical way. To make use of these files full of so much data, you need to choose an API to read, and in some cases, update and edit that data. Let’s check a few popular ones out now.

SAX(Simple API for XML) is a ‘read’-only API. You can only access and fetch pieces of data from the XML file. It’s a ‘push’ processor streaming API which does it’s work with the use of callback methods. SAX is fairly convenient to use in that it is fast and memory efficient, but it can become complex easily.

DOM(Document Object Model) is a ‘read and write’ style API. This API represents the entire XML document as a tree in memory. This makes DOM an attractive option for when you’re going to be searching the tree for individual elements. A downside is that it has a tendency to slowdown and be extremely memory sensitive, particularly when dealing with very large sets of data.

JAXB(Java Architecture of XML Binding) is the last API we’ll check out in this blog. Like DOM, it’s capable of reading and writing the data however it structures the XML it processes in an entirely different way. It draws the structure using annotations which are generated from XML schemas. It stores the entire document in memory to access quickly, however it doesn’t handle extremely large data sets as well as SAX.

In conclusion, there are a variety of XML parsing Java API’s out there and it’s important to consider the pros and cons which come with the use of all of them. In a majority of projects today, it’s my understanding that JAXB is the best choice, but that doesn’t mean it’s the end-all, be-all.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.