将架构位置添加到JAXB unmarshaller [英] Add schema location to JAXB unmarshaller

查看:121
本文介绍了将架构位置添加到JAXB unmarshaller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am facing the below error when JABX unmarshaller tries to unmarshall the xml

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

Exception in thread "main" javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 456; The prefix "xsi" for attribute "xsi:nil" associated with an element type "customerProductStatus" is not bound.]

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

When I looked at the xml being returned from the server , it is the below :

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

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

I don't see xsi defined in any of the parent tags. Is it possible to add the schemaLocation to unmarshaller without changing any of the bindings?

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;

这篇关于将架构位置添加到JAXB unmarshaller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆