Use Jaxb where possible

I was asked in an job interview, if I know something about DOM or SAX parsing. I know something about it, but I have only used it once. Why should I bother myself with this, when I just can use JAXB?
What is JAXB? I would say it is an XML Object Mapper.
JAXB is the simplest way to parse and write XML-files. Since Java 6 it is even included in the Java Runtime. JAXB of course needs a scheme as reference otherwise how should it now its mapping?

To use it you need data classes, which have JAXB annotations. The easiest way to do this, is to let JAXB generate it for you. You can even automatic this process in your build with various maven plugins.

And what about the Performance? The performance is great it even can outperform DOM. SAX is still better, but it may not worth the hassle to get the performance benefit. Yes if you have many files or very big files to process, then SAX is a better choice because of on the fly¬†processing and lower overhead, but usually you don’t need that.


Of course for simple XML files JAXB is too much. Here even I would just parse it using SAX.

Use JAXB where possible, especially for complex XML/XSD files. For simple files I would still use SAX, but JAXB can also be a good option.


Update 1

Clarified when SAX is useful.

This entry was posted in Java, tips and tagged , , . Bookmark the permalink.

Leave a Reply

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