users@jaxb.java.net

Re: question regarding unmarshal an element of XML

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 5 Nov 2007 22:14:50 +0100

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