aspose file tools*
The moose likes Web Services and the fly likes JAXB, DOM, SAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB, DOM, SAX" Watch "JAXB, DOM, SAX" New topic
Author

JAXB, DOM, SAX

Kodo Tan
Ranch Hand

Joined: Aug 14, 2001
Posts: 105
Anyone knows the difference between these 3 ways of reading XML files ? I mean when should we use JAXB instead of the other 2 and vice versa.
Must we always JAXB for webservices ?
Sorry for this silly questions as I'm very new to XML and webservices.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Anyone knows the difference between these 3 ways of reading XML files ? I mean when should we use JAXB instead of the other 2 and vice versa.
SAX and DOM are approaches for parsing an XML document while JAXB defines a binding between a specific XML schema and a corresponding Java object hierarchy.
Must we always JAXB for webservices ?
No.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Kodo Tan
Ranch Hand

Joined: Aug 14, 2001
Posts: 105
SAX and DOM are approaches for parsing an XML document while JAXB defines a binding between a specific XML schema and a corresponding Java object hierarchy.

Hi Lasse
Thanks for the reply .... sorry I'm still a bit confused here ... We can use SAX or DOM to parse an XML document and create the necessary objects or create a DOM tree and generate the XML document. These tasks can be done using JAXB too.
So my question is - "Since most of the things can be done by these 3 methods (SAX, DOM or JAXB), what are the main deciding criteria to determin which to use ?
Thanks and have a nice day.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
DOM and SAX work on a lower level than JAXB. Thus, if you need power/flexibility you should use DOM/SAX and write your own object-to-xml serialization/deserialization logic. Then again, if ease-of-use is important, you should first try whether JAXB can do it for you.
The JAXB FAQ also has an answer to your question.
Kodo Tan
Ranch Hand

Joined: Aug 14, 2001
Posts: 105
Hi
Thanks to all who replied ... I'm now clearer on the differences between JAXB and SAX/DOM.
Found the following from the sun's site. Hope that it's useful .
* JAXB simplifies access to an XML document from a Java program:
- JAXB allows you to access and process XML data without having to
know XML or XML processing. Unlike SAX-based processing, there's no
need to create a SAX parser or write callback methods.
- JAXB allows you to access data in non-sequential order, but unlike
DOM-based processing, it doesn't force you to navigate through a
tree to access the data.
- By unmarshalling XML data through JAXB, Java content objects that
represent the content and organization of the data are directly
available to your program.
* JAXB uses memory efficiently: The tree of content objects produced
through JAXB tends can be more efficient in terms of memory use than
DOM-based trees.
* JAXB is flexible:
- You can unmarshal XML data from a variety of input sources,
including a file, an InputStream object, a URL, a DOM node, or a
transformed source object.
- You can marshal a content tree to a variety of output targets,
including an XML file, an OutputStream object, a DOM node, or a
transformed data object
- You can unmarshal SAX events -- for example, you can do a SAX
parse of a document and then pass the events to JAXB for
unmarshalling.
- JAXB allows you to access XML data without having to unmarshal it.
Once a schema is bound you can use the ObjectFactory methods to
create the objects and then use set methods in the generated
objects to create content.
- You can validate source data against an associated schema as part
of the unmarshalling operation, but you can turn validation off if
you don't want to incur the additional validation overhead.
- You can validate a content tree, using the Validator class,
separately from marshalling. For example, you can do the
validating at one point in time, and do the marshalling at another
time.
* JAXB's binding behavior can be customized in a variety of ways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB, DOM, SAX