users@jaxb.java.net

Re: JAXB 2.1 - unmarshal out of context

From: Iaroslav Savytskyi <iaroslav.savytskyi_at_oracle.com>
Date: Fri, 07 Sep 2012 12:17:32 +0200

Hi, Scott,

Definitely it's possible to work with generics in JAXB 2.1.

But it's hard to say why do you have the problem without having more
info. The common approach to work with generics in JAXB is to use
adapters. Here is good example of it

http://stackoverflow.com/questions/826736/jaxb-marshalling-and-generics

May be it will be helpful for you.

--
Iaroslav
On 05/09/12 19:17, Shannon Scott wrote:
> Thank you.
> I removed the generateValueClass="false" from my bindings file - so now
> JAXB 2.1 does not generate the interfaces.
> I changed my generic class used to receive all types of xml during
> unmarshalling from an interface to an abstract class:
> package xxx.frameworks.interfaces;
>
> public class GenericXmlInterfaceRecord {
>
> }
>
> Now the unmarshaling fails with this error:
> unexpected element (uri:"", local:"Unit2Report"). Expected elements are
> (none)
>
> I imagine this is because my generic class does not have the XMLElement
> annotation:
> @XmlElement(name = " Unit2Report", required = true)
> (it is meant to be generic - not just for this set of elements)
>
> Is it possible to unmarshal to a generic class with JAXB 2.1 (like we
> did with JAXB 1.0)?
>
> Sorry if I missed the point of your suggestion.
> Thank you for trying to help.
> Shannon
>
>
> On Wed, Sep 5, 2012 at 11:17 AM, Wolfgang Laun <wolfgang.laun_at_gmail.com
> <mailto:wolfgang.laun_at_gmail.com>> wrote:
>
>     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
>     <mailto: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
>
>
>