0
我生成的类使用xjc,我试图处理下面的XML文档。 我得到的错误:JAXB minOccurs和UnmarshalException
javax.xml.bind.UnmarshalException: Unexpected end of element {http://schemas.xmlsoap.org/soap/envelope/}:Body
我相信这是因为XML不包含Fault元素(当我在一个错误元素添加,它的过程没有错误 响应要么包含RETRIEVAL_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>
模式:
<?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>
它的工作,感谢SOOOO多!!!!!!!!! – bmw0128 2011-02-23 21:48:51
我很好奇,我正在学习xml,xsd,jaxb等,你是如何知道这个答案的,我将如何在没有这个论坛的情况下学会这个答案? – bmw0128 2011-02-23 21:50:20
@ bmw0128 - 当我学习时,它是由同事组成的,只是尝试一些事情,Oracle(然后是Sun)文档,Google教程搜索,直到他们工作才尝试。 – justkt 2011-02-23 22:46:37