Paul Reiners
Paul Reiners

Reputation: 7894

Java to XSD or XSD to Java

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.

Upvotes: 7

Views: 12103

Answers (4)

bdoughan
bdoughan

Reputation: 149047

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.

Upvotes: 6

Petter Nordlander
Petter Nordlander

Reputation: 22279

If you have the opportunity to design both pojo and schema, It's a matter of design - do you design for a "perfect" schema or for a "perfect" java class.

In some cases, you don't have the luxury of a choice, in system integration scenarios, you might be given a predefined XSD from another system that you need to adapt to, then XSD -> Class will be the only way.

Upvotes: 1

DNA
DNA

Reputation: 42617

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...

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34387

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.,

Upvotes: 1

Related Questions