Java JAXB 解组不起作用。预期元素为(无)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20410202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB Unmarshalling not working. Expected elements are (none)
提问by Cameron Jones
I am trying to unmarshal an XML.
我正在尝试解组 XML。
This is what my XML looks like
这就是我的 XML 的样子
<DeviceInventory2Response xmlns="http://tempuri.org/">
<DeviceInventory2Result xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Obj123 xmlns="">
<Id>1</Id>
<Name>abc</Name>
</Obj123>
<Obj456 xmlns="">
.
.
.
I am trying to get Id and Name under Obj123. However when I run my unmarshal command I get the following error.
我正在尝试在 Obj123 下获取 Id 和 Name。但是,当我运行 unmarshal 命令时,出现以下错误。
An Error: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://tempuri.org/", local:"DeviceInventory2Response"). Expected elements are (none)
My code looks like this in the main class:
我的代码在主类中如下所示:
Obj123 myObj123 = (Obj123) unmarshaller.unmarshal(inputSource);
And my class for Obj123 looks like this:
我的 Obj123 类看起来像这样:
package com.myProj.pkg;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="Obj123")
public class Obj123 {
private String Id;
private String Name;
public String getId() {
return Id;
}
public String getName() {
return Name;
}
}
I thought by setting my XMLRootElement that I should be able to skip the first 2 lines of my XML but that doesn't seem to be happening. Any ideas?
我认为通过设置我的 XMLRootElement 我应该能够跳过我的 XML 的前两行,但这似乎没有发生。有任何想法吗?
Edit:
编辑:
This is how my JAXB Context is made:
这是我的 JAXB 上下文的制作方式:
JAXBContext jaxbContext = JAXBContext.newInstance();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Obj123 obj123 = (Obj123) unmarshaller.unmarshal(xmlStreamReader);
采纳答案by bdoughan
JAXB implementations will try to match on the root element of the document (not on a child element).
If you want to unmarshal to the middle of an XML document then you can parse the document with StAX advance the XMLStreamReader
to the desired element and then unmarshal that.
JAXB 实现将尝试匹配文档的根元素(而不是子元素)。如果您想解组到 XML 文档的中间,那么您可以使用 StAX 解析文档,XMLStreamReader
将其推进到所需的元素,然后对其进行解组。
For More Information
想要查询更多的信息
UPDATE
更新
now I am getting the following error. An Error: javax.xml.bind.UnmarshalException - with linked exception: [javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Obj123"). Expected elements are (none)].
现在我收到以下错误。错误:javax.xml.bind.UnmarshalException - 带有链接异常:[javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“Obj123”)。预期元素为(无)]。
A JAXBContext
only knows about the classes you tell it about. Instead of:
AJAXBContext
只知道你告诉它的类。代替:
JAXBContext jaxbContext = JAXBContext.newInstance();
You need to do:
你需要做:
JAXBContext jaxbContext = JAXBContext.newInstance(Obj123.class);
回答by Vibha
I solved the problem by adding
我通过添加解决了这个问题
@XmlRootElement(name="abc_xxx")
to the Root class.
(where abc_XXX is the root tag of your XML)
@XmlRootElement(name="abc_xxx")
到根类。
(其中 abc_XXX 是您的 XML 的根标记)
The JAXB classes generated by eclipse didn't add this annotation to my root class.
eclipse 生成的 JAXB 类没有在我的根类中添加这个注解。
回答by Sushil
Use ObjectFactory class instead like
使用 ObjectFactory 类代替
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
} catch (JAXBException e) {
e.printStackTrace();
}
JAXBElement<ObjectFactory> applicationElement = null;
try {
applicationElement = (JAXBElement<ObjectFactory>)
unmarshaller.unmarshal(Thread.currentThread().getClass()
.getResourceAsStream(fileName));
} catch (JAXBException e) {
e.printStackTrace();
}
Try this and will resolve above problem. My problem has been resolved.
试试这个,将解决上述问题。我的问题已经解决了。