Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB - Beans to XSD or XSD to beans?

Tags:

java

xml

jaxb

xsd

I have an existing data model. I would like to express this data model in terms of XML.

It looks like I have two options if I'm to use JAXB:

  • Create an XSD that mirrors my data model, and use xjc to create binding objects. Marshalling and unmarshalling will involve creating a "mapping" class that would take my existing data objects and map them to the objects that xjc created. For example, in my data model I have a Doc class, and JAXB would create another Doc class with basically the same exact fields, and I would have to map from my Doc class to xjc's Doc class.
  • Annotate my existing data model with JAXB annotations, and use schemagen to generate an XSD from my annotated classes.

I can see advantanges and disadvantages of both approaches. It seems that most people using JAXB start with the XSD file. It makes sense that the XSD should be the gold standard truth, since it expresses the data model in a truly cross-platform way.

I'm inclined to start with the XSD first, but it seems icky that I have to write and maintain a separate mapping class that shuttles data in between my world and JAXB world.

Any recommendations?

like image 359
bajafresh4life Avatar asked Mar 20 '10 14:03

bajafresh4life


People also ask

How do I generate Java classes from XSD using JAXB maven?

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.

What is use of XSD file 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.

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is JAXB marshalling and Unmarshalling?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.


2 Answers

Having the XSD generated from the existing classes sounds to me like the safest way to go.

However methinks that unless you know JAXB well then the approach with annotating your own classes may turn out to be extremely frustrating (in both pain and time :)).

This has happened to me in a related context when I tried to manually extract superclass from JAXB generated classes and then marshal the instances to XML. I was getting various (cryptic) JAXB exceptions. True, my JAXB knowledge is not that deep yet.

If you insist on using JAXB then I suggest to consider using the first approach (XSD + XJC) as a way to obtain initial JAXB annotations for your classes.

You can use XSD + XJC to get an idea how to annotate your own classes. Then you can try and fit the correct annotations onto them. Start with the more complex classes (references, inheritance, list of references, list of references to abstract base class) early on.

Using another technology to generate XSD from non-annotated classes as a kick start on the XSD may help. Or you may go for an XSD that covers most of what your classes use.

If the intention of this effort is to be also able to marshal the instances into XML, then I suggest to be on watch for JAXBElement. In some cases (which I cannot pin point due to lack of knowledge) instances will not marshal unless they are wrapped in a JAXBElement.

We use HyperJAXB to generate persistence tier based on a set of XSDs. The generated classes are also used for marshaling. We have plenty of 'fun' making this to work esp. because of IDREFs and JAXBElement.

like image 200
finrod Avatar answered Sep 30 '22 07:09

finrod


Reading between the lines, I believe the following is true:

  1. You have your desired object model.
  2. You have the desired XML schema, or atleast a pretty good idea of how you want it to look.

This scenario is usually referred to as "meet in the middle" mapping. It is only partially solved by JAXB. Luckily since JAXB is a specification, there are other implementations such as EclipseLink JAXB (MOXy) you can use.

MOXy offers an XPath based mapping extension that allows you to map between the Java model you have and the XML schema you want:

  • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
  • http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions
like image 34
bdoughan Avatar answered Sep 30 '22 07:09

bdoughan