javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Group")
unexpected element (uri:"", local:"Group"). Expected elements are <{}group>
Meet an exception when unmarshalling from xml
JAXBContext jc = JAXBContext.newInstance(Group.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Group group = (User)unmarshaller.unmarshal(new File("group.xml"));
Group class has no any annotation and group.xml just contains data.
Anything can be the cause?
It looks like your XML document has the root element "Group" instead of "group". You can:
- Change the root element on your XML to be "group"
- Add the annotation @XmlRootElement(name="Group") to the Group classs.
Luckily, the package-info class isn't required. I was able to fix mine problem with iowatiger08 solution.
Here is my fix showing the error message to help join the dots for some.
Error message
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://global.aon.bz/schema/cbs/archive/errorresource/0", local:"errorresource"). Expected elements are <{}errorresource>
Code before fix
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="", propOrder={"error"})
@XmlRootElement(name="errorresource")
public class Errorresource
Code after fix
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="", propOrder={"error"})
@XmlRootElement(name="errorresource", namespace="http://global.aon.bz/schema/cbs/archive/errorresource/0")
public class Errorresource
You can see the namespace added to @XmlRootElement as indicated in the error message.
You need to put package-info.java in your generated jaxb package. Its content should be something like that
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/StudentOperations/")
package generated.marsh;