将模式位置添加到JAXB解组器



当JABX解组器试图解组xml 时,我面临以下错误

线程"main"javax.xml.bind.UnmarshalException中出现异常-具有链接的异常:[org.xml.sax.SAXParseException;行号:1;列编号: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());

您可以添加以下内容:

//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;

最新更新