Skip to main content

JAXB and multiple XML documents (multiple schemas)

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2010-12-23

We are about to build an application that will receive different types of XML messages, each of them having its corresponding XML schema. These are some characteristics of the messages:

  1. All the XML messages have a root element tag named <Document>.
  2. Each message has a code that identifies it. The messages codes follow the format: business_area.message_number.variant_number.version_number. Example: "stmt.007.001.01", where "stmt" is "student management", "007" is the message number that identifies the type of message, "001" is the message variant, and "01" is the message version within the variant.
  3. The "xmlns" attribute of the root element <Document> holds the namespace for the XML message and the message code is part of the namespace name.

We are at Java 5 (in a near future moving to Java 6) and are exploring JAXB for ease of parsing, validating and generating XML content from the application.
However, when using "xjc" compiler to generate the Java classes, bearing in mind characteristic no. 1 previously pointed out, for each schema, a class, an class, and so on will be generated. I was wondering what the best way of organising the messages in Java packages would be. I was thinking of creating the classes for each XML schema in a package whose name is the message code, removing the dots. For example, JAXB classes derived from schema of message stmt.007.001.01 would be created into the Java package my.organisation.myproject.jaxb.stmt.stmt00700101. The fact that there may be different versions of one same message, I need to distinguish between versions, as the message will differ in some properties, field names, and/or length of those.
So when using JAXB and instantiating a context, should I use the constructor that takes semicolon-separated package names? What if I have 100 different messages/schemas?
With JAXB 2.0 the first element I'll get is a JAXBElement<Document>. Again, the fact that all XML messages share the root element's name ("Document"), and that the classes have nothing to do with each other (there is no inheritance relationship among them), should I use a switch/case statement to determine the class the instance Document is of? I need to know this because based on the class, I'll apply an XSLT transformation or another.
I've done my best to explain myself. Any hint or suggestion would be highly appreciated.