tags:

views:

3679

answers:

7

There is guy here swearing up and down that JAXB is the greatest thing since sliced bread. I am curious to see what stackoverflow users think the use case is for JAXB and what makes it a good or a bad solution for that case.

A: 

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

S.Lott
I think questions like this have an implied essay questionesque theme ala "Please answer in your own words."
Bob Cross
@Bob Cross: Vague questions mean no background reading at all or no specific problem to be solved. Both conditions make it very, very difficult to provide usable, actionable assistance. It's hard to determine "helpful" with such a vague question.
S.Lott
Though the question maybe vague, what I expect from StackOverflow is something more than the Google'd answer. At least, until Google returns mostly stackoverflow.com results.
Jay R.
@Jay R. What I expect from StackOverflow questions is something based on available information. If someone can't be bothered to read Google, I can't see why they're asking here. They still have to read and comprehend the answers.
S.Lott
Is there some information that I could add to the question to improve it?
Jay R.
@Jay R.: (1) Google JAXB. (2) Read a few links. Pay particular attention to the overview of JAXB **Already Provided by SUN/Oracle**. (3) After having done some reading -- especially the overview provided by SUN -- post your remaining questions about JAXB. Since there's already an overview and it already answers most of your question, it's seems silly to ask other folks to repeat that overview and introduce mistakes or confusion.
S.Lott
+5  A: 

It's an "ORM for XML". Most often used alongside JAX-WS (and indeed the Sun implementations are developed together) for WS Death Star systems.

Tom Hawtin - tackline
+4  A: 

With JAXB you can automatically create XML representations of your objects (marshalling) and object representations of the XML (unmarshalling).

As far as the XML Schema is concerned, you have two choices:

  • Generate Java classes from an XSD
  • Generate an XSD from your Java classes

There are also some simpler XML serialization libraries like XStream, Digester or XMLBeans that might be alternatives.

Fabian Steeg
These aren't binding frameworks, they are serialization libraries. A good binding framework can map any XML to an isomorphic object graph or back again. These simple serialization mechanisms produce and consume a fixed, non-standard XML schema.
erickson
Thanks erickson, I've only worked with JAXB and wanted to make sure I mention alternatives. I'll edit my answer based on your input.
Fabian Steeg
Actually, while XStream is ok, it's not really simpler, since both are very simple. And Digester is NOT simple, just simplistic.
StaxMan
Someone please mention JAXB-2.0 using Annotations. Between that an a few XmlAdapter classes, I can map any schema to any existing Java structure.
Mark Renouf
+12  A: 

I'm a big fan of JAXB for manipulating XML. Basically, it provides a solution to this problem (I'm assuming familiarity with XML, Java data structures, and XML Schemas):

Working with XML is difficult. One needs a way to take an XML file - which is basically a text file - and convert it into some sort of data structure, which your program can then manipulate.

JAXB will take an XML Schema that you write and create a set of classes that correspond to that schema. The JAXB utilities will create the hierarchy of data structures for manipulating that XML.

JAXB can then be used to read an XML file, and then create instances of the generated classes - laden with the data from your XML. JAXB also does the reverse: takes java classes, and generates the corresponding XML.

I like JAXB because it is easy to use, and comes with Java 1.6 (if you are using 1.5, you can download the JAXB .jars.) The way it creates the class hierarchy is intuitive, and in my experience, does a decent job abstracting away the "XML" so that I can focus on "data".

So to answer your question: I would expect that, for small XML files, JAXB might be overkill. It requires that you create and maintain an XML schema, and that you use the "standard textbook methods" of utilizing Java classes for data structures. (Main classes, small inner-classes to represent "nodes", and a huge hierarchy of them.) So, JAXB is probably not that great for a simple linear list of "preferences" for an application.

But if you have a rather complex XML schema, and lots of data contained within it, then JAXB is fantastic. In my project, I was converting large amounts of data between binary (which was consumed by a C program) and XML (so that humans could consume and modify that data). The resulting XML Schema was nontrivial (many levels if hierarchy, some fields could be repeated, others could not) so JAXB was helpful in being able to manipulate that.

rascher
JAXB can be done without XSD. You can use annotations: https://jaxb.dev.java.net/guide/Mapping_your_favorite_class.html
joekutner
JAXB is a spec (like JPA or JAXP), meaning that there are multiple implementations (Metro, EclipseLink, etc). If you have a problem with one you can switch to another implementation. You can also take advantage of extensions offered by different vendors.
Blaise Doughan
+6  A: 

Here's a reason not to use it: performance suffers. There is a good deal of overhead when marshaling and unmarshaling. You might also want to consider another API for XML-Object binding -- such as JiBX: http://jibx.sourceforge.net/

joekutner
Also consider using the tool XSD2Jibx if you're having trouble with bindings - it's in "alpha" status, but it's really helpful illustrating the classes and bindings it likes with XSD schemas...now, if only it stopped using SQL date by default, and used Java util Date instead...
MetroidFan2002
Bullshit. JAXB is pretty fast, as long as you use fast parser (like Woodstox), overhead of maybe 30-40% compared to manual binding. JiBX is fine, no problem with it, but performance difference is not huge.
StaxMan
I have used JAXB on several projects, and continue to use it. But its important to understand how it compares to other frameworks. If you are marshaling/unmarshaling documents that are hundreds of megabytes, and time is important - JiBX is a good alternative. JAXB == reflection, JiBX == byte code instrumentation.
joekutner
+3  A: 

JAXB is great if you have to code to some external XML spec defined as an XML schema (xsd).

For example, you have a trading application and you must report the trades to the Uber Lame Trade Reporting App and they've given you ultra.xsd to be getting on with. Use the $JAVA_HOME/bin/xjc compiler to turn the XML into a bunch of Java classes (e.g. UltraTrade).

Then you can just write a simple adapter layer to convert your trade objects to UltraTrades and use the JAXB to marshal the data across to Ultra-Corp. Much easier than messing about converting your trades into their XML format.

Where it all breaks down is when Ultra-Corp haven't actually obeyed their own spec, and the trade price which they have down as a xsd:float should actually be expressed as a double!

oxbow_lakes
Also note that you CAN do code-first: start with beans, generate schema iff you need it.
StaxMan
+2  A: 

I use JAXB at work all the time and I really love it. It's perfect for complex XML schemas that are always changing and especially good for random access of tags in an XML file.

I hate to pimp but I just started a blog and this is literally the first thing I posted about!

Check it out here: http://blog.gonigberg.com/2010/04/21/getting-started-with-jaxb/

artgon