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:
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?
Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.
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.
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.
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.
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.
Reading between the lines, I believe the following is true:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With