我正在重用其他地方生成的现有对象来解码作为字符串类型传入的 XML 数据。
对象:
/* 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: */ }
我的解码代码:
JAXBContext jc = JAXBContext.newInstance(MyNodeResponse.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyNodeResponse myNodeResponse = (MyNodeResponse)unmarshaller.unmarshal(new InputSource(new ByteArrayInputStream(xmlStringInput.getBytes("utf-8"))));
我的输入 XML:
<ns2:MyNodeResponse
xmlns:ns2="mynamespace/2010/10">
<ns2:Policy>
....more data....
<ns2:Policy/>
<ns2:MyNodeResponse />
解码时出现以下错误:
unexpected element (uri:"mynamespace/2010/10", local:"MyNodeResponse"). Expected elements are <{}MyNodeResponse>
“{ }”在错误中到底指的是什么,我如何解码以匹配输入 XML 中存在的内容以及对象的预期方式?
最佳答案
错误信息是什么意思
What exactly does the "{ }" refer to in the error
在 {}MyNodeRespons
中,{}
部分指的是未设置命名空间 URI 部分的限定名称。
如何修复
您需要使用包级 @XmlSchema
注释映射 namespace 限定:
package-info.java
@XmlSchema(
namespace = "mynamespace/2010/10",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
了解更多信息
关于java - JAXB 解码错误 : Expected elements are <{ } Root>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334990/