Search code examples
javaxsdjaxbpojo

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.


Solution

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