Thursday, August 23, 2007

A JAXB Problem
JAXBContext jc = JAXBContext.newInstance("edu.iu.cgl.gpir");
Unmarshaller u = jc.createUnmarshaller();
ResourceStatic rs = (ResourceStatic)u.unmarshal(new StreamSource(new StringReader(rslt)));

The above method works fine for GPIR while got an error for QBETS:
Exception in thread "main" java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to edu.ucsb.cs.nws.MachinesType

I debugged and found that the returned value of unmarshal() is JAXBElement, but don't know why it can't be cast to the type I expect.

Found the solution at http://www.velocityreviews.com/forums/t298335-javalangclasscastexception-javaxxmlbindjaxbelement.html

Use
MachinesType machines = (MachinesType)((JAXBElement)u.unmarshal(new StreamSource(new StringReader(rslt)))).getValue();

It works.