Java JAXB 解组错误:预期元素为 <{} Root>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22334990/
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 error: Expected elements are <{ } Root>
提问by JWiley
I'm reusing existing objects generated elsewhere to unmarshall XML data coming in as a String type.
我正在重用在其他地方生成的现有对象来解组作为 String 类型传入的 XML 数据。
The object:
物体:
/* 3: */ import java.util.ArrayList;
/* 4: */ import java.util.List;
/* 5: */ import javax.xml.bind.annotation.XmlAccessType;
/* 6: */ import javax.xml.bind.annotation.XmlAccessorType;
/* 7: */ import javax.xml.bind.annotation.XmlElement;
/* 8: */ import javax.xml.bind.annotation.XmlRootElement;
/* 9: */ import javax.xml.bind.annotation.XmlType;
/* 10: */
/* 11: */ @XmlAccessorType(XmlAccessType.FIELD)
/* 12: */ @XmlType(name="", propOrder={"policy"})
/* 13: */ @XmlRootElement(name="MyNodeResponse")
/* 14: */ public class MyNodeResponse
/* 15: */ {
/* 16: */ @XmlElement(name="Policy")
/* 17: */ protected List<Policy> policy;
/* 18: */
/* 19: */ public List<Policy> getPolicy()
/* 20: */ {
/* 21:65 */ if (this.policy == null) {
/* 22:66 */ this.policy = new ArrayList();
/* 23: */ }
/* 24:68 */ return this.policy;
/* 25: */ }
/* 26: */ }
My unmarshalling code:
我的解组代码:
JAXBContext jc = JAXBContext.newInstance(MyNodeResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyNodeResponse myNodeResponse = (MyNodeResponse)unmarshaller.unmarshal(new InputSource(new ByteArrayInputStream(xmlStringInput.getBytes("utf-8"))));
My input XML:
我的输入 XML:
<ns2:MyNodeResponse
xmlns:ns2="mynamespace/2010/10">
<ns2:Policy>
....more data....
<ns2:Policy/>
<ns2:MyNodeResponse />
I get the following error when unmarshalling:
解组时出现以下错误:
unexpected element (uri:"mynamespace/2010/10", local:"MyNodeResponse"). Expected elements are <{}MyNodeResponse>
What exactly does the "{ }" refer to in the error, and how do I unmarshall in a way to match what is present in the input XML and how the object is expecting?
“{}”在错误中究竟指的是什么,我如何解组以匹配输入 XML 中存在的内容以及对象的期望?
采纳答案by bdoughan
What the Error Message is Saying
错误信息说的是什么
What exactly does the "{ }" refer to in the error
“{}”在错误中究竟指的是什么
In {}MyNodeRespons
the {}
portion refers to that qualified name not having the namespace URI portion set.
在{}MyNodeRespons
该{}
部分指的是不具有命名空间URI部组限定名。
How to Fix It
如何修复
You need to map the namespace qualification using the package level @XmlSchema
annotation:
您需要使用包级别@XmlSchema
注释映射命名空间限定:
package-info.java
包信息.java
@XmlSchema(
namespace = "mynamespace/2010/10",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For More Information
想要查询更多的信息