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?

332862 次浏览

看起来您的 XML 文档的根元素是“ Group”而不是“ Group”。您可以:

  1. 将 XML 上的根元素更改为“ group”
  2. 将注释@XmlRootElement (name = “ Group”)添加到 Group 类。

您需要将 package-info.java 放到生成的 jaxb 包中,它的内容应该是这样的

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.org/StudentOperations/")
package generated.marsh;

在查看更多内容之后,根元素必须与模式名称空间相关联,Blaise 注意到了这一点。然而,我没有一个包-信息 java。因此,在不使用@XMLSchema 注释的情况下,我能够通过使用

@XmlRootElement (name="RetrieveMultipleSetsResponse", namespace = XMLCodeTable.NS1)
@XmlType(name = "ns0", namespace = XMLCodeTable.NS1)
@XmlAccessorType(XmlAccessType.NONE)
public class RetrieveMultipleSetsResponse {//...}

Hope this helps!

我也是。映射类的名称是 Mbean,但标记的根名称是 mbean,因此我必须添加注释:

@XmlRootElement(name="mbean")
public class MBean { ... }

Luckily, the package-info class isn't required. I was able to fix mine problem with iowatiger08 solution.

这里是我的修复,显示了错误消息,以帮助加入一些点。

Error message

意外的元素 (uri:"http://global.aon.bz/schema/cbs/archive/errorresource/0", Local: “ errorresource”)。预期的元素是 < {} errorresource >

修正前的代码

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="", propOrder={"error"})
@XmlRootElement(name="errorresource")
public class Errorresource

一个接一个的代码

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="", propOrder={"error"})
@XmlRootElement(name="errorresource", namespace="http://global.aon.bz/schema/cbs/archive/errorresource/0")
public class Errorresource

您可以看到添加到@XmlRootElement 的名称空间,如错误消息所示。

I had the same problem.. It helped me, I'm specify the same field names of my classes as the tag names in the xml file (the file comes from an external system).

例如:

我的 xml 文件:

<Response>
<ESList>
<Item>
<ID>1</ID>
<Name>Some name 1</Name>
<Code>Some code</Code>
<Url>Some Url</Url>
<RegionList>
<Item>
<ID>2</ID>
<Name>Some name 2</Name>
</Item>
</RegionList>
</Item>
</ESList>
</Response>

My Response class:

@XmlRootElement(name="Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlElement
private ESList[] ESList = new ESList[1]; // as the tag name in the xml file..


// getter and setter here
}

我的 ESList 课程:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="ESList")
public class ESList {
@XmlElement
private Item[] Item = new Item[1]; // as the tag name in the xml file..


// getters and setters here
}

我的物品类别:

@XmlRootElement(name="Item")
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlElement
private String ID; // as the tag name in the xml file..
@XmlElement
private String Name; // and so on...
@XmlElement
private String Code;
@XmlElement
private String Url;
@XmlElement
private RegionList[] RegionList = new RegionList[1];


// getters and setters here
}

我的区域列表类:

@XmlRootElement(name="RegionList")
@XmlAccessorType(XmlAccessType.FIELD)
public class RegionList {
Item[] Item = new Item[1];


// getters and setters here
}

返回文章页面我的拆解课程:

public class DemoUnmarshalling {
public static void main(String[] args) {
try {
File file = new File("...");


JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setEventHandler(
new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event ) {
throw new RuntimeException(event.getMessage(),
event.getLinkedException());
}
}
);


Response response = (Response) jaxbUnmarshaller.unmarshal(file);


ESList[] esList = response.getESList();
Item[] item = esList[0].getItem();
RegionList[] regionLists = item[0].getRegionList();
Item[] regionListItem = regionLists[0].getItem();


System.out.println(item[0].getID());
System.out.println(item[0].getName());
System.out.println(item[0].getCode());
System.out.println(item[0].getUrl());
System.out.println(regionListItem[0].getID());
System.out.println(regionListItem[0].getName());


} catch (JAXBException e) {
e.printStackTrace();
}
}
}

它给出了:

1
Some name 1
Some code
Some Url
2
Some name 2

这是一个相当利基用例的修复,但它让我每次。如果您正在使用 Eclipse Jaxb 生成器,那么它将创建一个名为 package-info 的文件。

@javax.xml.bind.annotation.XmlSchema(namespace = "blah.xxx.com/em/feed/v2/CommonFeed")
package xxx.blah.mh.domain.pl3xx.startstop;

如果您删除这个文件,它将允许解析一个更通用的 xml!

如果上述方法都不起作用,请尝试添加

@XmlRootElement(name="Group")到集团类。

我也有同样的问题。 我向 <xs:schema..>添加了以下属性 ElementFormDefault = “修饰的”属性 FormDefault = “未修饰的”

<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)

这为我解决了问题。

您需要将 package-info.java类放在 contextPath的包中,并将下面的代码放在同一个类中:

@javax.xml.bind.annotation.XmlSchema(namespace = "https://www.namespaceUrl.com/xml/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.test.valueobject;

我已经有同样的问题,我只是改变如下:

@XmlRootElement -> @XmlRootElement(name="Group")

I had the same issue, my problem was, that I had two different webservices with two different wsdl-files. I generated the sources in the same package for both webservices, which seems to be a problem. I guess it's because of the ObjectFactory and perhaps also because of the package-info.java - because those are only generated once.

我解决了这个问题,在不同的包中为每个 Web 服务生成源代码。这样,您还有两个不同的 ObjectFactory 和 package-info.java 文件。

如果你发疯了,因为这只发生在你的测试和你正在使用 PowerMock 这是解决方案,添加到你的测试类的顶部:

@PowerMockIgnore({ "javax.xml.*", "org.xml.*", "org.w3c.*" })

这里提到的所有解决方案都不适合我,我仍然得到:

线程“ main”javax.xml.bind 中的异常: 意外元素(uri: “ java: XXX.XX.XX.XXX”,local: “ XXXXX”)

经过大量的研究,通过其他网站下面的代码为我工作-

FileInputStream fis = new FileInputStream("D:/group.xml");
SOAPMessage message = factory.createMessage(new MimeHeaders(), fis);
JAXBContext jc = JAXBContext.newInstance(Group.class);
Unmarshaller u = jc.createUnmarshaller();
JAXBElement<Group> r = u.unmarshal(message.getSOAPBody().extractContentAsDocument(), Group.class);
Group group = r.getValue();

所以当我使用来自不是我的 xsd 文件的预生成类时,出现了异常。

生成的 xml-object 名称和名称空间(@xmlroot)在 java 类中丢失了,因此我无法解组它。 像这样将缺少的 URI 放在类的上面“解决”了问题:

@XmlRootElement(name = "XHE", namespace = "http://docs.oasis-open.org/bdxr/ns/XHE/1/ExchangeHeaderEnvelope")
public class XHEType {

但是因为我希望这个类只存在于 target 中,所以这是不够的。 我最后想到的是这个新类,它将生成的类包装在@xmlroot 中:

@XmlRootElement(name = "XHE", namespace = "http://docs.oasis-open.org/bdxr/ns/XHE/1/ExchangeHeaderEnvelope")
public class XHEType2  extends XHEType {

还有执法官守则:

        JAXBContext jaxbContext = JAXBContext.newInstance(XHEType2.class);
javax.xml.bind.Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
XHEType xheType = (XHEType) unmarshaller.unmarshal(reader);