Take the 2-minute tour ×

I have been using DOM for parsing my small xml docs for sometime.Afer reading about the JAXB (http://www.oracle.com/technetwork/articles/javase/index-140168.html), I'm planning to use JAXB instaed of DOM.

Please let me know if this will be a right approach.

share|improve this question

5 Answers 5

up vote 30 down vote accepted

JAXB is not directly comparable to DOM and SAX. The Java DOM and SAX parsing APIs are lower-level APIs to parse XML documents, while JAXB (Java API for XML Binding) is a higher-level API for converting XML elements and attributes to a Java object hierarchy (and vice versa). Implementations of JAXB will most likely use a DOM or SAX parser behind the scenes to do the actual parsing of the XML input data.

Often you'll want to convert the content of an XML document into objects in your Java program. If this is what you want to do, then JAXB will probably be easier to use and you'll need to write less code than when you would be using the DOM or SAX parsing API.

Whether it's the right approach for your case depends on exactly what the functional and technical requirements are for your project.

share|improve this answer

JAXB will be easy from the sense of less code writing. But if you need more control on parsing you should use SAX parser . It gives you more control over parsing and it faster then DOM parser. Apart from it , JAXB is high level API,so it has some overhead task so it will be bit slower then SAX.

share|improve this answer

Check this post What is JAXB and why would I use it? Its always better to use JAXB over the conventional DOM and SAX parser.

share|improve this answer
"always better"? I strongly disagree. For example with huge inputs, SAX has significant advantages over both DOM and JAXB. Also XML that allows arbitrary elements can be tricky to handle with JAXB. –  Joachim Sauer Oct 10 '11 at 8:28
@Joachim Sauer thanks for correcting my post –  kunal Oct 10 '11 at 8:36
Check out this post dublintech.blogspot.co.uk/2011/12/jaxb-sax-dom-performance.html It turns out that with JDK 7 any implementation of JAXB is more performant than DOM! –  PiersyP Sep 13 '12 at 1:37

JAXB, stands for Java Architecture for XML Binding. JAXB is used to Marshalling(Java objects to XML representations) and Unmarshalling (XML contents to Java object)

please follow this link for good understanding of JAXB Architecture

share|improve this answer

JAXB don't support XPath. You need a schema to generate the api. If you have a unknown or a potential bad XML your program end by validation error. Not good for error tolerant XML processing, if you want only parsing not validation. Not good for processing unknown XML and looking for known node structures inside. Navgation in complicate XML Structures produce a lot of bad readable code.

If you use JAXB you implement a fix coupling between the XML structure and your java code. If XML is changed you must change your code and deploy it. May be painful. If you use XPath you can complete decoupling XML-structur and java code, if XPath is in external configuration.

share|improve this answer
If you mean my answer is not useful, please show me an example how can good readable navigate in XML structures with deep > 3. An how simply iterate over childs of a node? In XML-DOM and XPath no problem - simple! JAXB is best for simple XML and if you don't want think about ( or you can't) complex XML structures. –  Gerd Jan 16 at 14:26

Your Answer


