Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java to XSD or XSD to Java

Tags:

java

jaxb

pojo

xsd

I know that, using JAXB, you can generate Java files from an XSD and that you can also generate the XSD from annotated POJOs. What are the advantages and disadvantages of each? Is one overall better than the other?

We basically want to serialize events to a log in XML format.

like image 678
Paul Reiners Avatar asked Oct 17 '12 20:10

Paul Reiners


3 Answers

Ultimately it depends on where you want to focus:

If the XML Schema is the Most Important Thing

Then it is best to start from the XML schema and generate a JAXB model. There are details of an XML schema that a JAXB (JSR-222) implementation just can't generate:

  • A maxOccurs other than 0, 1, or unbounded
  • many facets on a simple type
  • model groups

If the Object Model is the Most Important Thing

If you will be using the Java model for more than just converting between objects and XML (i.e. using it with JPA for persistence) then I would recommend starting with Java objects. This will give you the greatest control.

like image 118
bdoughan Avatar answered Oct 15 '22 08:10

bdoughan


It depends on your requirement and scenario with respect to the point of initiation.

Given your requirement, use generate Java files from an XSD as you want to define the output(XML) format first which should be supported by Java.,

like image 22
Yogendra Singh Avatar answered Oct 15 '22 08:10

Yogendra Singh


Given that one of the main points of XML is to be a portable data-transfer format, usable regardless of platform or language, I would avoid generating XSD from any specific programming language, as a rule of thumb. This may not matter if you know you are only communicating between Java endpoints (but are you sure this will be true forever?).

It is better, all else being equal, to define the interface/schema in a programming-language neutral way.

There are lots of exceptions to this general principle, especially if you are integrating with existing or legacy code...

like image 1
DNA Avatar answered Oct 15 '22 06:10

DNA