Xml JAXB minOccurs和unmarshaleException
我使用xjc生成类,并尝试处理以下XML文档。 我得到一个错误:Xml JAXB minOccurs和unmarshaleException,xml,xsd,jaxb,Xml,Xsd,Jaxb,我使用xjc生成类,并尝试处理以下XML文档。 我得到一个错误: javax.xml.bind.UnmarshalException: Unexpected end of element {http://schemas.xmlsoap.org/soap/envelope/}:Body 我相信这是因为XML不包含Fault元素(当我添加Fault元素时,它会处理无错误的数据)。 响应将包含检索ID或错误,但决不能同时包含两者。我认为在模式中使用minOccurs=0可以解决此问题,但无法解决(至
javax.xml.bind.UnmarshalException: Unexpected end of element {http://schemas.xmlsoap.org/soap/envelope/}:Body
我相信这是因为XML不包含Fault元素(当我添加Fault元素时,它会处理无错误的数据)。
响应将包含检索ID或错误,但决不能同时包含两者。我认为在模式中使用minOccurs=0可以解决此问题,但无法解决(至少我是如何做到的)。
有没有可能在这种情况下使用JAXB,也就是说,当这些元素中的任何一个可能存在,但决不能同时存在这两个元素
有问题的XML响应:
<?xml version = '1.0' encoding = 'UTF-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<env:Header>
<bmw:rule xmlns:bmw="http://adr.com/bmw">
<bmw:customer>44</bmw:customer>
<bmw:schemaName>ABC</bmw:schemaName>
<bmw:schemaVersion>1.0</bmw:schemaVersion>
</bmw:rule>
</env:Header>
<env:Body>
<bmw:RETRIEVAL_ID xmlns:bmw="http://adr.com/bbs">15086</bmw:RETRIEVAL_ID>
</env:Body>
</env:Envelope>
44
基础知识
1
15086
模式:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bmw="http://adr.com/bmw">
<xs:import namespace="http://adr.com/bmw" schemaLocation="bmw.xsd"/>
<xs:element name="Envelope">
<xs:complexType>
<xs:sequence>
<xs:element ref="env:Header"/>
<xs:element ref="env:Body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Header">
<xs:complexType>
<xs:sequence>
<xs:element ref="bmw:rule"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Body">
<xs:complexType>
<xs:sequence>
<xs:element ref="bmw:RETRIEVAL_ID" minOccurs="0"/>
<xs:element ref="env:Fault" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Fault">
<xs:complexType>
<xs:sequence>
<xs:element ref="bmw:fault"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
现在,您的complexType包含:
<xs:complexType>
<xs:sequence>
<xs:element ref="bmw:RETRIEVAL_ID" minOccurs="0"/>
<xs:element ref="env:Fault" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
在更改模式后,需要使用xjc重新生成类文件,以便在Java中反映更改
有一个关于JAXB如何处理choice
的小部分。有一个相当全面的choice
和JAXB示例。需要注意的重要一点是:
@XmlElements(value = {
@XmlElement(name="RETRIEVAL_ID",
type=RetrievalID.class),
@XmlElement(name="FAULT",
type=Fault.class)
})
Object possibleValue;
在你的Java中。我很好奇,我正在学习xml、xsd、jaxb等,你是如何知道这个答案的,我是如何在这个论坛上学到这个答案的?@bmw0128-当我学习时,是同事们的组合,只是尝试一些东西,Oracle(然后是Sun)文档,谷歌搜索教程,然后尝试一些东西,直到它们成功为止。
@XmlElements(value = {
@XmlElement(name="RETRIEVAL_ID",
type=RetrievalID.class),
@XmlElement(name="FAULT",
type=Fault.class)
})
Object possibleValue;