users@jaxb.java.net

Re: question regarding unmarshal an element of XML

From: Qingtian Wang <qingtian.wang_at_gmail.com>
Date: Mon, 5 Nov 2007 17:32:35 -0600

Ok, I am feeling really dumb here :( But I can't figure out how this
works the other way. i.e. Marshalling a sub-element.

When I do:

marshaller.marshal(instanceOfBoundBean2.getSomeElement(), aStringWriter);

I get

==========
Caused by: com.sun.istack.SAXException2: unable to marshal type
"com.ual.hcp.poc.schemas.generated.jaxb.ApolloOpenSessionRQ$Request"
as an element because it is missing an @XmlRootElement annotation
        at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:244)
        at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:303)
        at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:490)
        at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:328)
        ... 33 more
===========

I read the Javadoc of the Marshaller where it says the object being
passed in either has to be annotated as @XmlRootElement, or has to a
be "wrapped" to an JAXBElement. And I have no idea of either:

1. The Java class is generated by xjc, am I supposed to annotate it
with @XmlRootElement by hand?

2. I don't understand how come "instanceOfBoundBean2.getSomeElement()"
is NOT of type "JAXBElement". And if it is really not, how do I "wrap"
it into one?

Thanks again!





On 11/5/07, Qingtian Wang <qingtian.wang_at_gmail.com> wrote:
> Lexi- Thanks so much!!
>
> On 11/5/07, Aleksei Valikov <valikov_at_gmx.net> wrote:
> > Hi
> >
> > > ====
> > >
> > > Hi,
> > >
> > > I should be able to unmarshal a sub-element of XML using the unmarshal
> > > method, right?
> > >
> > > Here's my problem: I am using the latest 2.1 impl.
> > >
> > > I bound my XSDs into jaxb classes
> > >
> > > mypack/BoundBean1.java
> > > mypack/BoundBean2.java
> > > ....
> > >
> > > Then in my client class, I tried to unmarshal a sub-Element of XML per
> > > BoundBean2.xsd like this:
> > >
> > > ....
> > > (Bean2.SomeElement) jc.createUnmarshaller().unmarshal(new
> > > StringReader(xmlElementStringOfBoundBean2));
> > > ....
> > >
> > > I got an exception like this:
> >
> > ...
> >
> > See this:
> > http://java.sun.com/webservices/docs/2.0/api/javax/xml/bind/Unmarshaller.html#unmarshalByDeclaredType
> >
> > You'll need to do something like:
> >
> > unmarshall(mySource, Bean2.SomeElement.class);
> >
> > You'll get a JAXBElement where value is the value (Bean2.SomeElement)
> > you expect.
> >
> > Bye.
> > /lexi
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> > For additional commands, e-mail: users-help_at_jaxb.dev.java.net
> >
> >
>