Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Unmarshall exception - unexpected element

I have used a .xsd file to generate Java classes, and with an XML file, I need to unmarshall.

I am using this code :

JAXBContext objJAXBContext = JAXBContext.newInstance("my.test");

// create an Unmarshaller
Unmarshaller objUnmarshaller = objJAXBContext.createUnmarshaller();

FileInputStream fis = new FileInputStream("test.xml");

JAXBElement<Root> objMyRoot = (JAXBElement<Root>) objUnmarshaller.unmarshal(fis);

Root mRoot = objMyRoot.getValue();

and I am getting this error:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Root"). Expected elements are (none)

I have seen many solutions but nothing works in my project.

What i can try to do?

like image 910
Vítor Nóbrega Avatar asked Mar 02 '12 11:03

Vítor Nóbrega


Video Answer


1 Answers

Your xml root is missing the namespace (uri) attribute. You better try this on the XMLRootElement ...

@XmlRootElement(name = "root", namespace="")
like image 138
Waqas Memon Avatar answered Jan 06 '23 02:01

Waqas Memon