簡體   English   中英

我如何分開<xsd:choice />使用 JAXB 將子元素放入單獨的集合屬性中?

[英]How do I separate the <xsd:choice/> sub-elements into individual Collection properties using JAXB?

我有以下來自供應商的XSD片段,我無法更改它的指定方式:

<xsd:element name="navmap">
  <xsd:complexType>
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element ref="navitem"/>
      <xsd:element ref="debug"/>
    </xsd:choice>
    <xsd:attribute name="focus" type="xsd:string" use="optional"/>
  </xsd:complexType>
</xsd:element>

現在沒有自定義它會生成以下代碼

@XmlElements({
        @XmlElement(name = "navitem", type = Navitem.class),
        @XmlElement(name = "debug", type = Debug.class)
    })
    protected List<Object> navitemOrDebug;

我寧願它為每種類型生成一個單獨的列表,如下所示

@XmlElements({
        @XmlElement(name = "navitem", type = Navitem.class)
    })
    protected List<Navitem> navitems;

@XmlElements({
        @XmlElement(name = "debug", type = Debug.class)
    })
    protected List<Debug> debugs;

我的.xjb文件中有以下內容,它重命名了整個List ,但我不知道如何拆分它們。

<jxb:bindings node="//xsd:element[@name='navmap']//xsd:complexType//xsd:choice">
  <jxb:property name="contents" />
</jxb:bindings>

如何為外部.xjb綁定文件中的每個類型指定一個單獨的ListSet

如果我不能這樣做,如何將<jaxb:annotation/>添加到.xsd文件中,以便為每種類型指定單獨的ListSet

我不在乎訂購,在這種特殊情況下順序並不重要。

注意:我更喜歡外部.xjb解決方案,我不想將 custom.xsd 與供應商提供的每個新版本進行比較,它們太多了。

正如所承諾的,請滿足Simplify Plugin

這個插件允許簡化“復雜”的屬性。 這些屬性通常是從像這樣的可重復選擇中生成的:

<xs:complexType name="typeWithReferencesProperty">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="a" type="someType"/>
        <xs:element name="b" type="someType"/>
    </xs:choice> 
</xs:complexType>

...

<xs:complexType name="typeWithElementsProperty">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="a" type="xs:string"/>
        <xs:element name="b" type="xs:int"/>
    </xs:choice> 
</xs:complexType>

默認情況下,XJC 將復雜的屬性建模幾個引用或元素合二為一。

@XmlElementRefs({
    @XmlElementRef(name = "a", type = JAXBElement.class),
    @XmlElementRef(name = "b", type = JAXBElement.class)
})
protected List<JAXBElement<SomeType>> aOrB;

...

@XmlElements({
    @XmlElement(name = "a", type = String.class)
    @XmlElement(name = "b", type = Integer.class),
})
protected List<Serializable> aOrB;

這些復數屬性要求XML 模式的復數內容充分,即保持可重復選擇中元素的順序。 不幸的是,它們不像 bean 屬性那樣慣用。 這些屬性是“異構的”(從某種意義上說它們存儲不同的類型),這使得它們很難使用。

但是,如果元素的順序不重要——也就是說,您可以接受它在重新編組后會發生變化的事實,則可以簡化這些屬性的結構:可以將復雜的屬性拆分為幾個簡單的屬性。

Simplify Plugin 實現了這個任務。 它允許您簡化復雜的屬性。 該插件將刪除復雜屬性並插入幾個更簡單的屬性而不是原始(復雜)屬性。 所以你可以得到類似的東西:

@XmlElement(name = "a", type = String.class)
protected List<String> a;
@XmlElement(name = "b", type = Integer.class)
protected List<Integer> b;

或者:

@XmlElement(name = "a")
protected List<SomeType> a;
@XmlElement(name = "b")
protected List<SomeType> b;

或者:

@XmlElementRef(name = "a", type = JAXBElement.class)
protected List<JAXBElement<SomeType>> a;
@XmlElementRef(name = "b", type = JAXBElement.class)
protected List<JAXBElement<SomeType>> b;

取決於定制。

該插件將在JAXB2 Basics 0.6.3 中發布,現在可作為此存儲庫中的快照使用。

據我所知,目前你不能(使用來自 JAXB RI 的 XJC)。 從理論上講,必須可以編寫一個插件來更改 XJC model。

但是您必須注意,兩個單獨的同質屬性debugnavitem不等於一個異質屬性navitemOrDebug 即,如果您解組和編組相同的 object,您將獲得不同的元素順序。

但是,在許多情況下,這可能是有道理的。 在此處提出問題,我會考慮實施它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM