java JAXB Unmarshaller - 意外元素异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16815418/
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 Unmarshaller - Unexpected element Exception
提问by rawkfist0215
I'm using the JAXB parser to convert XML sent via an http request to a Java object while validating it against my XSD schema. The problem is that when the unmarshal() method is called it raises this exception:
我正在使用 JAXB 解析器将通过 http 请求发送的 XML 转换为 Java 对象,同时根据我的 XSD 模式对其进行验证。问题在于,当调用 unmarshal() 方法时,它会引发此异常:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.somedomain.com/", local:"assign"). Expected elements are (none)
javax.xml.bind.UnmarshalException:意外元素(uri:“ http://www.somedomain.com/”,本地:“assign”)。预期元素为(无)
If I remove the namespace from my root XML element it raises the same exception with the uri portion being empty:
如果我从我的根 XML 元素中删除命名空间,它会引发相同的异常,其中 uri 部分为空:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"assign"). Expected elements are (none)
javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“assign”)。预期元素为(无)
The unmarshalling code:
解组代码:
ServletInputStream xmlFile = request.getInputStream();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("PatientAssignment.xsd"));
JAXBContext jc = JAXBContext.newInstance(AssignType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new AssignValidationEventHandler(patientResponses));
assignments = (AssignType) unmarshaller.unmarshal(xmlFile);
My Java class and package-info.java:
我的 Java 类和 package-info.java:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AssignType", namespace = "http://www.somedomain.com/", propOrder = {
"patient"
})
public class AssignType {
@XmlElement(namespace = "http://www.somedomain.com/", required = true)
protected List<PatientAssignType> patient;
/* Getters and setters ommitted */
}
// package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.somedomain.com/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
The XML I'm trying to parse:
我试图解析的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<assign xmlns="http://www.somedomain.com/">
<patientAssign xmlns="http://www.somedomain.com/">
<firstName>Buddy</firstName>
<lastName>Holly</lastName>
<email></email>
<dob></dob>
<phone></phone>
...
</patientAssign>
</assign>
If anyone could tell me where I'm going wrong, it'd be much appreciated!
如果有人能告诉我哪里出错了,将不胜感激!
回答by Pace
You will need an @XmlRootElement
annotation on your AssignType
class. You'll probably also want to add name=patientAssign
to your patient instance variable.
您将需要@XmlRootElement
对您的AssignType
班级进行注释。您可能还想添加name=patientAssign
到您的患者实例变量中。