将架构位置添加到JAXB unmarshaller

时间:2013-09-01 14:49:51

标签: java xml jaxb unmarshalling jaxb2

当JABX unmarshaller尝试解组xml时,我遇到以下错误

线程“main”中的异常javax.xml.bind.UnmarshalException   - 链接异常: [org.xml.sax.SAXParseException; lineNumber:1; columnNumber:456;与元素类型“customerProductStatus”关联的属性“xsi:nil”的前缀“xsi”未绑定。]

当我查看从服务器返回的xml时,它是以下内容:

<customerProductStatus xsi:nil = "true"></customerProductStatus>

我没有在任何父标签中看到xsi定义。是否可以在不更改任何绑定的情况下将schemaLocation添加到unmarshaller?

JAXBContext jaxbContext1 = JAXBContext.newInstance(Request.class);
Unmarshaller jaxbUnMarshaller1 = jaxbContext1.createUnmarshaller();
Request request = (Request)jaxbUnMarshaller1.unmarshal(receiveData.getBinaryStream());

1 个答案:

答案 0 :(得分:2)

您可以添加:

//Gets schema
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(xmlSchema);

JAXBContext jaxbContext1= JAXBContext.newInstance(Request.class);
Unmarshaller jaxbUnMarshaller1 = jaxbContext1.createUnmarshaller();

//Sets schema with unmarshaller
jaxbUnMarshaller1 .setSchema(schema);

Request request = (Request)jaxbUnMarshaller1.unmarshal(receiveData.getBinaryStream());

所需的套餐是:

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;