users@jaxb.java.net

Re: Creating JAXBContext with Multiple Package References

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Tue, 26 Apr 2011 08:49:45 +0200

Javadoc of JAXBContext.newInstance( String contextPath,...):

"The client application must supply a context path which is a list of
colon (':', \u003A) separated java package names that contain
schema-derived classes..."

JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )

-W

On 26 April 2011 00:25, Justin Randall <randju_at_hotmail.com> wrote:
>
> 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
>
>