tags:

views:

404

answers:

2

Hi,

I have a XML schema that I will need to create Java classes for. It is not a particularly large schema, I'd say it will result in around 20 classes. I am trying to weigh up whether to use an automatic binding program (like the one supplied in JAXB or JiBX) or whether to manually write my own classes and use something like XStream for marshalling/unmarshalling.

What are the advantages/disadvantages of writing your own classes as opposed to using a binding program.

Also, one I use a binding program, am I tied to that forever. For example, if I use JAXB's binding compiler to create the classes, do I have to use JAXB for all marshalling/unmarshalling?

p.s. I've seen the following questions about XML binding/serialization, which were useful, but didn't answer my question totally: xml-serialization-in-java and java-xml-binding

+11  A: 

I don't think there is a definitive answer to your question. But I can give you some hard won advice. Here are some things to consider:

  1. It is time consuming to write marshalling and unmarhsalling code, especially the first time.

  2. You will have to spend quite a bit of time learning the nuances of your DOM library (Xerces or its equivalent).

  3. There is lots of duplication so you'll eventually be driven to writing some helper classes.

  4. You'll need lots of unit tests to make sure you've covered all of your bases in the area of optional elements and attributes.

Looking at that list it is pretty easy to say "that's what JAXB does for me". Having done for this for a number of years I would say that JAXB saves you quite a bit of time and effort, especially the latest iteration of JAXB in Java 5/6.

But if you go with JAXB there is one lesson we've learned the hard way that I would like to pass along:

*** Do not to let the JAXB generated classes leak into your application.

As you said in your question, this ties your entire application to the way JAXB does thing. If JAXB has to be replaced (there are a number of reason why you might do that in the future) then you will be faced with a challenging and painful task (trust me, we've done it and we'll never get into that position again).

Now we always hide our JAXB generated classes behind a facade or a factory, mapping from the JAXB classes to our own domain POJOs that have the required behavior. We think of JAXB as we do JDBC; JAXB is just another data source, another way of getting data to and from our domain POJOs. The domain POJOs are the secret sauce and we control how they are coded and how they are used. JAXB is just a tool for marshalling and unmarshalling.

Biff
"It is time consuming to write marshalling and unmarhsalling code, especially the first time." - True, and I would add the corollary "You will get it wrong at least once."
Marcus Downing
A: 

I do not want to start a long and endless holly war on the question but from my experience - if you can use tool that generates all code for you and do not require JAR dependable. The code that will be generated will be ugly but it will work until you will clearly see a need to handcraft the handwritten code. In most cases you will not need to do that. I am sorry I can not give an instant recommendation on what tool to use.