JAXB :Class cast exception while trying to unmarshall XML using JAXB
Posted
by Navin
on Stack Overflow
See other posts from Stack Overflow
or by Navin
Published on 2010-05-04T06:22:40Z
Indexed on
2010/05/04
6:28 UTC
Read the original article
Hit count: 655
jaxb2.1
Hi
I am novice to JAXB , i am trying to sample using JAXB.
trying to dispaly the values in the MenuList.xml
----MenuList.xml-----------
Projects
Library
Library1
----------------------------MenuList.xsd-------------------
The uisng the command I run the xsd file and it generated the classes. MenuList and Object Factory.
AppTest.java
package com.xx.menu;
import java.io.File; import java.io.IOException;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.UnmarshalException; import javax.xml.bind.Unmarshaller;
public class TestNew {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
JAXBContext jc = JAXBContext.newInstance("com.xx.menu"); //Create unmarshaller
Unmarshaller um = jc.createUnmarshaller();
File file = new File ("C:\\sample\\menulist.xml");
JAXBElement element = (JAXBElement)um.unmarshal(file);
Menulist menulist= (Menulist) element.getValue ();
System.out.println("name : "+menulist.getMenu());
}
catch( UnmarshalException ue ) {
ue.printStackTrace();
System.out.println( "Caught UnmarshalException" );
} catch( JAXBException je ) {
je.printStackTrace();
} catch( Exception ioe ) {
ioe.printStackTrace();
}
}
}
Error:
java.lang.ClassCastException: com.xx.menu.Menulist at com.xx.menu.TestNew.main(TestNew.java:26)
Can you please help me where I am going wrong ...I will be greatly thankful to you.
Thanks
© Stack Overflow or respective owner