0

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

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

当我查看从服务器返回的 xml 时,如下所示:

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

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

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

1 回答 1

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;
于 2013-09-01T15:11:28.250 回答