gpt4 book ai didi

java - 将架构位置添加到 JAXB 解码器

转载 作者:行者123 更新时间:2023-12-02 06:46:42 31 4
gpt4 key购买 nike

当 JABX 解码器尝试解码 xml 时,我遇到以下错误

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

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

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

我没有看到任何父标记中定义了 xsi。是否可以将 schemaLocation 添加到解码器而不更改任何绑定(bind)?

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;

关于java - 将架构位置添加到 JAXB 解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18559552/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com