JAXB Unmarshall exception - unexpected element

I used the .xsd file to generate Java classes, and with the XML file I need to undo the marker.

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 get 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 can I do?

+7
source share
2 answers

Your xml root does not have a namespace attribute (uri). Better try this on XMLRootElement ...

 @XmlRootElement(name = "root", namespace="") 
+16
source

Try

 StreamSource streamSource = new StreamSource("test.xml") JAXBElement<Root> objMyRoot = (JAXBElement<Root>) objUnmarshaller.unmarshal(streamsource, Root.class); 
+4
source

All Articles