2011-05-10 66 views
2

您好我有定義爲這樣一個模式:JAXB的EclipseLink問題與的nillable元素編組CHOICE

<complexType name="x"> 
    <sequence> 
     <element name="year" type="date"/> 
       <choice> 
        <element name="comuneNascita" type="string" nillable="true"/> 
        <element name="statoNascita" type="string" nillable="true"/> 
       </choice> 
    </sequence> 
</complexType> 

當我嘗試馬歇爾與XJC(與XJC:簡單的選項)生成的類,我得到這樣的結果:

[...] 
    <statoNascita xsi:nil="true"/> 
    <comuneNascita>xxx</comuneNascita> 
    [...] 

這是錯的!

刪除nillable =「true」解決了這個問題,但後來我必須指定一個有效的元素(未被刪除)。 任何解決方法?

回答

0

您可以通過其標註爲財產避免你的問題如下:

@XmlElements({ 
    @XmlElement(name="comuneNascita", type=String.class), 
    @XmlElement(name="statoNascita", type=String.class), 
}) 

你可以得到XJC生成註釋爲上述使用JAXB綁定文件屬性:

<?xml version="1.0" encoding="UTF-8"?> 
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
      version="2.1"> 
    <globalBindings choiceContentProperty="true"/> 
</bindings> 

對於更多信息

+0

只有當您不使用選項來避免某些屬性的JaxElement類型生成時,纔會出現這種情況。在這種情況下,選項choiceContentProperty =「true」將被忽略。 爲了避免這個問題,我不得不改變的模式,因爲這: <選擇的minOccurs = 「0」> \t <元素名稱= 「comuneNascita」 類型= 「字符串」/> \t <元素名稱= 「statoNascita」 類型= 「string」/> 所以元素null時不用xsi:nil編組。但有時候改變架構不是一種選擇。 – Massimo 2011-05-12 07:17:49