Reputation: 1213
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?
Upvotes: 7
Views: 14732
Reputation: 1249
Your xml root is missing the namespace (uri) attribute.
You better try this on the XMLRootElement
...
@XmlRootElement(name = "root", namespace="")
Upvotes: 16
Reputation: 4356
Try
StreamSource streamSource = new StreamSource("test.xml")
JAXBElement<Root> objMyRoot = (JAXBElement<Root>) objUnmarshaller.unmarshal(streamsource, Root.class);
Upvotes: 4