• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAXB, DOM, SAX

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Kodo Tan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic