aspose file tools*
The moose likes XML and Related Technologies and the fly likes JAXP(SAX & DOM) vs JAXB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXP(SAX & DOM) vs JAXB" Watch "JAXP(SAX & DOM) vs JAXB" New topic
Author

JAXP(SAX & DOM) vs JAXB

Sara Jahan
Ranch Hand

Joined: Apr 20, 2003
Posts: 120
If this was a competition, which one you think would be the winner?

Sara
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
In a way, JAXB is a bundle flavoured over DOM/SAX techniques.
Logically i would choose what best fits my requirements and time.


Spritle Software Blogs
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

What prize would they be competing for?
Sara Jahan
Ranch Hand

Joined: Apr 20, 2003
Posts: 120
Originally posted by Paul Clapham:
What prize would they be competing for?


I would use the winner in my dev work, I guess!

The thing is I am trying to educate myself about JAXB. Most of the white papers/tutorials start with a brief intro on sax/dom where the writers have unfavourably compared them to jaxB - i.e. how simple and better JAXB is as compared to jaxP.

I was hence wondering, if that is the case then why people are still using sax/dom. I guess that I will have to find out myself after I finish with jaxB.

Sara
[ January 15, 2006: Message edited by: Sara Jahan ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
This would be a competition of apples and oranges. JAXB is a Java <--> XML binding mechanism, i.e. it creates Java objects from XML files and vice-versa, and is implemented on top of JAXP. SAX and DOM can be used for a variety of purposes where JAXB would be a complete mis-fit.

Of course, if it is the Java binding that you actually need, then JAXB makes that much simpler than it would be using JAXP.


Ping & DNS - my free Android networking tools app
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Talking about which XML tech is "better" is a useless exercise since XML is used over such a huge range of applications.
Sudha Rams
Greenhorn

Joined: May 13, 2005
Posts: 12
With reference to Ulf Dittmer's post in this thread, I've got a question:

From what I've understood, JAXB is used by Java web services implementation to convert user-defined data classes to equivalent XML representation and vice-versa, thus enabling tier-tier communication in the web services environment.

Is there anything else to JAXB apart from this? I'm just trying to appreciate the use of JAXB from the business perspective i.e. where JAXB comes into play to solve a business problem like say process data and generate a report.

For example, suppose I've got an XML (that has an XML schema) with student grades data and I need to process this data to generate a report (which again is represented in an XML format).

Using DOM API, I can traverse the nodes in this XML using XPATH, process data and generate another XML as a report.

Can I do the same for the content tree created by the JAXB unmarshaller, without marshalling that tree to a DOM node?? If no, then what is the point? Does that mean that, in this example case, DOM should suffice and usage of JAXB may not be of any value-add or will not even make sense?

Please let me know! :roll:

Also, if you can think of a business problem that JAXB is ideally suited to solve, please share it.

Thanks for your time!
[ January 17, 2006: Message edited by: Sudha Rams ]
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
JAXB has nothing to do with DOM. The idea of JAXB is that you can have an XML Schema definition of your types, and JAXB will create the Java object representations of those types, plus the marshalling and demarshalling helper code needed to get you from the Java world to the XML world and back.

The reason for using JAXB is that you've already decided you want to use or are willing to use XML Schema and you want a Java object representation of your XML that is closely in sync with the XML structure. The "benefit" is more from XML Schema than JAXB; you get more constraint power than you do with simple XML validation. The situation is very different from, for example, SAX, where Java representation of XML is irrelevant because SAX is only concerned with firing events when particular XML artifacts are detected during parsing.


Reid - SCJP2 (April 2002)
Sudha Rams
Greenhorn

Joined: May 13, 2005
Posts: 12
Thanks for your explanation, Reid. Better late than never!
RayZ Wilson
Greenhorn

Joined: Jan 22, 2010
Posts: 3
I feel a need to reiterate what someone said above. JAXB is mainly used for web services. It allows you to work in the Java realm without having to worry about XML at all practically. The SOAP messages sent to the web service consist of an XML representation of the objects you are interacting with. You are interacting with an object model not actual XML. When the web service replies you interact with it as instances of objects as well. You never really have to do any parsing the web services interface does it all for you. JAXB gives you tools to convert WSDL to java objects which represent the web service end points.


Keep imagining, keep inventing, stay ingenious.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Inside JAXB and DOM implementations there is always SAX. SAX code is always at the foundation of any code using Java-based XML parsers.
Andrew Kid
Greenhorn

Joined: Jan 26, 2010
Posts: 1
James, can you give some prooflinks?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
JAXB is mainly used for web services.

No, it is used (and usable) in lots of places. The fact that JAX-WS uses JAXB under the hood says nothing about JAXB's potential for applications elsewhere. Java <-> XML bindings ceased to be a hot topic a couple of years ago once lots of capable frameworks for doing it become available; WS just brought it back to the front pages.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JAXP(SAX & DOM) vs JAXB