javax.xml.bind.UnmarshalException: unexpected elem

2019-01-16 12:16发布

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?

标签: java xml jaxb
9条回答
冷血范
2楼-- · 2019-01-16 12:46

I had the same issue. I added following attributes to <xs:schema..> elementFormDefault="qualified" attributeFormDefault="unqualified"

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.com/schemas/ArrayOfMarketWithStations"
    targetNamespace="http://www.example.com/schemas/ArrayOfMarketWithStations" 
    elementFormDefault="qualified" attributeFormDefault="unqualified" >

and re-generated java classes by running xjc, which corrected package-info.java.

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.com/schemas/ArrayOfMarketWithStations", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)

This fixed the issue for me.

查看更多
做自己的国王
3楼-- · 2019-01-16 12:47

If none of the above works, try adding

@XmlRootElement(name="Group") to the Group classs.

查看更多
小情绪 Triste *
4楼-- · 2019-01-16 12:50

Same to me. The name of the mapping class was Mbean but the tag root name was mbean so I had to add the annotation:

@XmlRootElement(name="mbean")
public class MBean { ... }
查看更多
登录 后发表回答