2011-02-23 66 views
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> 

回答

0

現在您的複雜類型包含:

<xs:complexType> 
    <xs:sequence> 
    <xs:element ref="bmw:RETRIEVAL_ID" minOccurs="0"/> 
    <xs:element ref="env:Fault" minOccurs="0"/> 
    </xs:sequence> 
</xs:complexType> 

如果只有一個出現在特定的時間,你想xs:choice而不是xs:sequence。有關更多信息,請參閱this

<xs:complexType> 
    <xs:choice> 
    <xs:element ref="bmw:RETRIEVAL_ID" minOccurs="0"/> 
    <xs:element ref="env:Fault" minOccurs="0"/> 
    </xs:choice> 
</xs:complexType> 

在更改架構以更改爲反映在Java中之後,您將需要使用xjc重新生成類文件。

This article from Oracle有關於JAXB如何處理choice的小節。 This blog post有一個相當全面的例子choice和JAXB。重要的是要注意的是,您將擁有:

@XmlElements(value = { 
      @XmlElement(name="RETRIEVAL_ID", 
         type=RetrievalID.class), 
      @XmlElement(name="FAULT", 
         type=Fault.class) 
    }) 
Object possibleValue; 

在您的Java中。

+0

它的工作,感謝SOOOO多!!!!!!!!! – bmw0128 2011-02-23 21:48:51

+1

我很好奇,我正在學習xml,xsd,jaxb等,你是如何知道這個答案的,我將如何在沒有這個論壇的情況下學會這個答案? – bmw0128 2011-02-23 21:50:20

+2

@ bmw0128 - 當我學習時,它是由同事組成的,只是嘗試一些事情,Oracle(然後是Sun)文檔,Google教程搜索,直到他們工作才嘗試。 – justkt 2011-02-23 22:46:37