Where I can find a detailed comparison of Java XML frameworks?

yegor256 picture yegor256 · Oct 4, 2010 · Viewed 18k times · Source

I'm trying to choose an XML-processing framework for my Java projects, and I'm lost in names.. XOM, JDOM, etc. Where I can find a detailed comparison of all popular Java XML frameworks?

Answer

Aravind Yarram picture Aravind Yarram · Dec 27, 2010

As Blaise pointed out stick with the standards. But there are multiple standards created over the period to solve different problems/usecases. Which one to choose completely depends upon your requirement. I hope the below comparison can help you choose the right one.

Now there are two things you have to choose. API and the implementations of the API (there are many)

API

SAX: Pros

  • event based
  • memory efficient
  • faster than DOM
  • supports schema validation

SAX: Cons

  • No object model, you have to tap into the events and create your self
  • Single parse of the xml and can only go forward
  • read only api
  • no xpath support
  • little bit harder to use

DOM: Pros

  • in-memory object model
  • preserves element order
  • bi-directional
  • read and write api
  • xml MANIPULATION
  • simple to use
  • supports schema validation

DOM: Cons

  • memory hog for larger XML documents (typically used for XML documents less than 10 mb)
  • slower
  • generic model i.e. you work with Nodes

Stax: Pros

  • Best of SAX and DOM i.e. Ease of DOM and efficiency of SAX
  • memory efficient
  • Pull model
  • read and write api
  • supports subparsing
  • can read multiple documents same time in one single thread
  • parallel processing of XML is easier

Stax: Cons

  • no schema validation support (as far as I remember, not sure if they have added it now)
  • can only go forward like sax
  • no xml MANIPULATION

JAXB: Pros

  • allows you to access and process XML data without having to know XML
  • bi-directional
  • more memory efficient than DOM
  • SAX and DOM are generic parsers where as JAXB creates a parser specific to your XML Schmea
  • data conversion: JAXB can convert xml to java types
  • supports XML MANIPULATION via object API

JAXB: Cons

  • can only parse valid XML

Trax: For transforming XML from 1 form to another form using XSLT

Implementations

SAX, DOM, Stax, JAXB are just specifications. There are many open source and commercial implementations of these specifications. Most of the time you can just stick with what comes with JDK or your application server. But sometimes you need to use a different implementation that provided by default. And this is where you can appreciate the JAXP wrapper api. JAXP allows you to switch implementations through configuration without the need to modify your code. It also provides a parser/spec independent api for parsing, transformation, validation and querying XML documents.

Performance and other comparisons of various implementations


Now standards are good but once in a while you encounter this crazy usecase where you have to support parsing of XML document that is 100 gigabytes of size or you need ultra fast processing of XML (may be your are implementing a XML parser chip) and this is when you need to dump the standards and look for a different way of doing things. Its about using the right tool for the right job! And this is where I suggest you to have a look at vtd-xml

During the initial days of SAX and DOM, people wanted simpler API's than provided by either of them. JDOM, dom4j, XmlBeans, JiBX, Castor are the ones I know that became popular.