Java for XSD or XSD for Java

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

Basically we want to serialize events in a log format in XML format.

+7
source share
4 answers

Ultimately, it depends on where you want to focus:

If XML Schema is the Most Important Thing

Then it’s best to start with an XML schema and create a JAXB model. There are details of the XML schema that the JAXB implementation (JSR-222) simply cannot generate:

  • Maximum values ​​other than 0, 1 or unlimited
  • many faces on a simple type
  • model groups

If the object model is the most important thing

If you will use the Java model for more than simple conversion between objects and XML (i.e. using it with JPA to save), I would recommend starting with Java objects. This will give you maximum control.

+6
source

It depends on your requirement and scenario regarding the initiation point.

Given your requirement, use generate Java files from an XSD as you want to first determine the output format (XML) that Java must support.,

+1
source

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

It is better, ceteris paribus, to define an interface / circuit in a neutral way in a programming language.

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

+1
source

If you have the opportunity to design both pojo and schema, this is a design question - you are designing an β€œideal” scheme or an β€œideal” java class.

In some cases, you do not have the luxury of choice, in system integration scenarios you may be provided with a predefined XSD from another system that you need to adapt to, then XSD β†’ Class will be the only way.

+1
source

All Articles