Posts

Showing posts from March, 2013

JAXBContext - ClassCastException

I was recently working with REST like APIs (e.g. text/xml content type response from a web request). In order to consume or use the response i need to parse the xml response .We usually use an ordinary STaX ( SAX Vs. STAX ) parser because it performs really well and we usually parse really huge XML files that contains thousands of elements. In the API that i am consuming the XML is really small about ~10 elements. So i decided to use JAXB (comes standard with Java 1.6) because XML is really small and I have to display the result of the service to a UI. After creating my javabeans ( javabeans ) and annotating it with JAXB. I initialized my jaxbContext this way (Code 1.1) and tried unmarshalling an xml. As you notice below i have the same root element for Foo and Bar xml . This is causing the JAXBContext to parse the Foo xml to a Bar class that is why i get a ClassCastException. I have to change the root elements for the xml and the JAXB @XMLRootElement annotation as well so that i