users@jaxb.java.net

Re: JAXB 2.1 - unmarshal out of context

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 5 Sep 2012 17:17:20 +0200

JAXB 2.0 doesn't handle interfaces. You'll have to provide classes.
-W


On 5 September 2012 16:52, Shannon Scott <sscott_at_mapit.net> wrote:

> I am trying to upgrade from JAXB 1.0 to JAXB 2.1 (in Java 1.6).
>
> We have a directory where the jaxb generated files are:
> xxx.interfaces.unit2.data
>
> We have a generic class used to receive all types of xml during
> unmarshalling:
> package xxx.frameworks.interfaces;
>
> public interface GenericXmlInterfaceRecord {
>
> }
> ## JAXB 1.0 ##
> Our JAXBContext and UnMarshaller are intialized like this:
> _jaxbContext = JAXBContext.newInstance("xxx.interfaces.unit2.data");
> _unMarshaller = _jaxbContext.createUnmarshaller();
> _unMarshaller.setEventHandler(_xmlValidationEventsHandler);
> _unMarshaller.setValidating(true);
>
> Previously we would unmarshal our xml like this:
>
> GenericXmlInterfaceRecord record =
> (GenericXmlInterfaceRecord) _unMarshaller.unmarshal(
> new StreamSource(new StringReader(xmlString)));
> We would extend our generic class in the generated JAXB class for the
> root element (for example):
> package xxx.interfaces.unit2.data;
>
> public interface Unit2ReportType
> extends
> javax.xml.bind.Element, xxx.frameworks.interfaces.GenericXmlInterfaceRecord
> {
>
>
> ## JAXB 2.1 ##
> We are intializing the JAXBContext and UnMarshaller like this:
> _jaxbContext = JAXBContext.newInstance("xxx.interfaces.unit2.data");
> _unMarshaller = _jaxbContext.createUnmarshaller();
> _unMarshaller.setEventHandler(_xmlValidationEventsHandler);
> _unMarshaller.setSchema(schema);
>
> And we are trying to unmarshal our xml like this:
> JAXBElement<GenericXmlInterfaceRecord>
> rootElement = _unMarshaller.unmarshal(new StreamSource(
> new StringReader(xmlString)),GenericXmlInterfaceRecord.class);
>
> GenericXmlInterfaceRecord record = rootElement.getValue();
>
> But this throws an error:
> xxx.frameworks.interfaces.GenericXmlInterfaceRecord is not known to this
> context
>
> I think the error is because the GenericXmlInterfaceRecord is
> not within the JAXBContext used to create the unmarshaler
> ("xxx.interfaces.unit2.data").
>
> QUESTION:
> Is there a way with JAXB 2.1 to unmarshal to a class outside the
> directory/package with the JAXB generated files (like we did with JAXB
> 1.0)?
>
> I also tried adding the GenericXmlInterfaceRecord package name to the
> JAXBContext initialization, and putting @XmlRootElement into
> the GenericXmlInterfaceRecord class, but then I got this error:
> ClassNotFoundException: xxx.frameworks.interfaces.impl.ObjectFactory
>
> Any help or advice is greatly appreciated.
> Sorry if this is not the appropriate venue for the inquery, feel free to
> direct me elsewhere.
>
> Thank you.
> Shannon
>