Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Binding Vs Manually Defining Classes

Tags:

java

xml

binding

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

like image 723
Lehane Avatar asked Nov 04 '08 11:11

Lehane


People also ask

What is XSD and XJB?

The global schema binding declarations in bindings. xjb are the same as those in po. xsd for the Datatype Converter example. The only difference is that because the declarations in po. xsd are made inline, you need to embed them in <xs:appinfo> elements, which are in turn embedded in <xs:annotation> elements.

Why XSD is used in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

What is the use of XJB file?

xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

What is view binding and Data Binding?

View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: View binding requires no annotation processing, so compile times are faster.


1 Answers

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.

like image 97
Biff Avatar answered Oct 17 '22 05:10

Biff