Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB vs DOM and SAX

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.

like image 789
Habin Avatar asked Oct 10 '11 07:10

Habin


People also ask

Which is faster DOM or SAX?

DOM Parser is faster than SAX Parser.

What is the difference between SAX and DOM based XML processing?

DOM stands for Document Object Model while SAX stands for Simple API for XML parsing. DOM parser load full XML file in-memory and creates a tree representation of XML document, while SAX is an event based XML parser and doesn't load whole XML document into memory.

What can I use instead of JAXB?

XOM, JDOM, dom4j, etc. etc. Projects like Castor and Apache XMLBeans predate JAXB, so you could have a look at those. Ulf Dittmer wrote: XOM, JDOM, dom4j, etc.


3 Answers

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.

like image 170
Jesper Avatar answered Oct 10 '22 04:10

Jesper


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

like image 4
Mohasin Ali Avatar answered Oct 10 '22 05:10

Mohasin Ali


(~7 years too late, but still)

To contradict the accepted answer, I highly doubt JAXB is implemented on top of DOM - that would require two Object Models (Java and DOM) and would entirely defeat the point of XML binding (the XB in JAXB) worse for performance/space it would require both models to back each other.

Seems to me JAXB is a clear analog of DOM when considering approaches to handling XML bi-directional mapping in Java. DOM and JAXB are just cases of what IBM calls XOM.

In almost all cases I'd favour JAXB over DOM, and favour handrolled over SAX - primarily because I find the SAX and DOM API's "language agnostic" bent utterly grotesque.


Obviously if your dataset is too big to fit into memory then an Object Model isn't viable. Otherwise

How to choose:

  • Have a Java model, or are loading one from XML? it's serialization, just use JAXB
  • No Java model, time/space paramount, simple single pass attribute/element querying, schema unknown? Use SAX
  • No Java model and time/space paramount but schema is known, unchanging and simple? Roll your own, seriously it's trivial (however do use an XSD and JAXB and codegen to test your implementation)
  • Is XML schema unknown a priori, subject to change without notification, or permits arbitrary unknown (namespaced) extensions? Use DOM
  • Is XML schema locked or you own it? Use JAXB
like image 9
earcam Avatar answered Oct 10 '22 05:10

earcam