Search code examples
javaxmljaxb

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Group")


I encountered an exception when unmarshalling from XML:

    unexpected element (uri:"", local:"Group"). Expected elements are <{}group>

Group class has no annotations and group.xml just contains data.

    JAXBContext jc = JAXBContext.newInstance(Group.class); 
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Group group = (User)unmarshaller.unmarshal(new File("group.xml"));

What could be the cause?


Solution

  • It looks like your XML document has the root element "Group" instead of "group". You can:

    1. Change the root element on your XML to be "group"
    2. Add the annotation @XmlRootElement(name="Group") to the Group classs.