JAXB - Beans to XSD or XSD to beans?

Posted by bajafresh4life on Stack Overflow See other posts from Stack Overflow or by bajafresh4life
Published on 2010-03-20T14:47:50Z Indexed on 2010/03/20 14:51 UTC
Read the original article Hit count: 404

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about jaxb

Related posts about Xml