7

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.

Community
  • 1
  • 1
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202

4 Answers4

6

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.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • We're using JAXB to transfer data to and from REST Web Services. Sometimes we might already have beans/DTOs available that we could use to generate XSDs. Sometimes we will be starting from scratch and creating these beans. My guess is that our object model is the most important thing in these cases, do you agree? The XSD would be used for nothing more than a step in creating objects for transferring data. – medloh Sep 17 '14 at 16:00
1

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

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

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

DNA
  • 42,007
  • 12
  • 107
  • 146
  • 2
    There is nothing Java specific about the XML schema generated by a JAXB (JSR-222) implementation. – bdoughan Oct 17 '12 at 20:57
  • 2
    Good. However, it's my experience that one often ends up with a quite different schema if one starts from a programming language, than if one starts from XSD, due to the different styles and features of the languages. Starting with a schema editor also tends to produce different designs (better factored, but less readable) than if one starts with example XML then reverse-engineers a schema from that. – DNA Oct 17 '12 at 21:01
  • I've seen well designed, hand crafted and valid schemas that import well by JAXB but fail to generate classes for .NET 4. I've never seen that happend with schemas generated from JAXB classes though. You are of course right that the approach tend to give different schemas. Specially, deep structured XML gives pojos that is a mess to create from scratch in code. – Petter Nordlander Oct 17 '12 at 21:11
  • Presumably schemas generated from POJOs could have the same issue (i.e. if .NET doesn't follow the standard, almost any non-.NET approach to schema generation isn't guaranteed to interoperate with it)? – DNA Oct 17 '12 at 21:13
  • In theory, yes. But in practice my experience has to do with reuse of references. Never seen JAXB generate that issue, yet. – Petter Nordlander Oct 17 '12 at 21:19
1

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.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84