users@jaxb.java.net

Creating JAXBContext with Multiple Package References

From: Justin Randall <randju_at_hotmail.com>
Date: Mon, 25 Apr 2011 18:25:04 -0400

Hello,

I have an XML schema project which attempts to develop a generic XML schema for implementing XML-based messaging services.

The idea is that the same key XML structures for messages, operations, results are re-used by individual implementations XML messaging services for various autonomous systems. The "parent" XML XSD is GenericXmlMessaging.xsd (namespace "http://www.example.com/GenericXmlMessaging") which contains two abstract complexTypes: GenericXmlRequest and GenericXmlResponse. An example implementation is ExampleImplXmlMessaging.xsd (namespace "http://www.example.com/ExampleImpl") which contains various extensions of GenericXmlRequest/GenericXmlResponse representing actual operations/responses (i.e. [type="LoginRequest", base="GenericXmlRequest"], [type="LoginResponse", base="GenericXmlResponse"], [type="LogoutRequest", base="GenericXmlRequest"], [type="LogoutResponse", base="GenericXmlResponse"], etc.).

When compiling the XSDs into Java no errors occur and everything under namespace "http://www.example.com/GenericXmlMessaging" is packaged under "com.example.www.xml" and everything under namespace "http://www.example.com/ExampleImpl" is packaged under "com.example.www.exampleimpl".

The problem I'm having is when trying to marshall/unmarshall XML content. When creating a JAXBContext using newInstance ("com.example.www.exampleimpl"), attempting to marshall an XML message throws an exception because GenericXmlRequest/GenericXmlResponse aren't defined, however when creating a JAXBContext using newInstance ("com.example.www.xml") attempting to marshall an XML message succeeds, however the actual content from the "http://www.example.com/ExampleImpl" is empty. If I recompile the XSDs to produce everything under a single java package "com.example.www.exampleimpl" and use that package name when creating the new JAXBContext then everything works fine.

Is it possible to specify multiple java package names when creating JAXBContext instances? Are there any other solutions I'm unaware of that would allow a single JAXBContext to marshall/unmarshall objects belonging to multiple namespaces/java-packages?

Regards,

Justin Randall