java.lang.ClassCastException: javax.xml.bind.JAXBElement

M

marco

Hi,

this problem occurs using JAXB with for unmarshalling an XML file.
The code is:

JAXBContext jc = JAXBContext.newInstance("Resource");
Unmarshaller u = jc.createUnmarshaller();

//u.setValidating(true); //deprecated!!

MyResType res = (MyResType)u.unmarshal(new FileInputStream("foo.xml"));


Anyone can help?

Thanks
 
O

Oliver Wong

marco said:
Hi,

this problem occurs using JAXB with for unmarshalling an XML file.
The code is:

JAXBContext jc = JAXBContext.newInstance("Resource");
Unmarshaller u = jc.createUnmarshaller();

//u.setValidating(true); //deprecated!!

MyResType res = (MyResType)u.unmarshal(new FileInputStream("foo.xml"));

Use something like:

System.err.println(u.umarshal(new
FileInputStream("foo.xml")).getClass().getName());

to get the actual type that's being returned, or use a debugger to
inspect the variable.

- Oliver
 
M

marco

Thank you Oliver,

javax.xml.bind.JAXBElement is the actual type that is being returned.

I've read the documentation, but I wasn't able to solve this problem.

Oliver Wong ha scritto:
 
O

Oliver Wong

[post re-ordered]

marco said:
Oliver Wong ha scritto:


javax.xml.bind.JAXBElement is the actual type that is being returned.

You probably marshalled your data incorrectly then. Show the marshalling
code.

- Oliver
 
M

marco

JAXBContext jax = JAXBContext.newInstance("Resource");
Marshaller m = jax.createMarshaller();
MyResType myRes = obj.createMyResType();
myRes.setBody("body"); //simple xsd:strings here
myRes.setHeader("header");

m.marshal(myRes, new FileOutputStream("foo.xml"));


thanks a lot!

Oliver Wong ha scritto:
[post re-ordered]

marco said:
Oliver Wong ha scritto:


javax.xml.bind.JAXBElement is the actual type that is being returned.

You probably marshalled your data incorrectly then. Show the marshalling
code.

- Oliver
 
Joined
Jun 20, 2006
Messages
1
Reaction score
0
Hi,

I had the same problem.
I realized that the unmarshal(...) method return an JAXBElement not the Object that I was expecting.
So, try this:

JAXBContext jc = JAXBContext.newInstance("Resource");
Unmarshaller u = jc.createUnmarshaller();
MyResType res =
(MyResType) ((JAXBElement) u.unmarshal(new FileInputStream("foo.xml"))).getValue();

Probably it will work.
If not you can always do something like

Object o = u.unmarshal(new FileInputStream("foo.xml"));
System.out.println(o.getClass().getName);

to retrive the real object that the unmarshal(...) is returning.
 
Joined
May 18, 2007
Messages
1
Reaction score
0
You can also have this casting error at run time if you are using and xml file that won't validate with the schema you built the JAXBContent package with. Ask yourself, does the XML I am unmarshalling match the schema I'm thinking of?<p>-Quinn Carver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top