3

いくつかの JAXB xjc.exe 生成クラスを単純な XML クラスに変換しようとしています。動的要素に注釈を付ける方法がわかりません。たとえば、スキーマには次のものがあります。

<!-- Message Set Request/Response Pairs and contained requests  -->
<xsd:element name="QBXMLMsgsRq">
    <xsd:complexType>
        <xsd:choice minOccurs="0" maxOccurs="unbounded">
            <xsd:element name="HostQueryRq" type="HostQueryRqType"/>
            <xsd:element name="CompanyQueryRq" type="CompanyQueryRqType"/>
            <xsd:element name="CompanyActivityQueryRq" type="CompanyActivityQueryRqType"/>
            <!-- many more of these choices -->
        </xsd:choice>
        <xsd:attribute name="oldMessageSetID" type="STRTYPE"/>
        <!-- some other attributes -->
    </xsd:complexType>
</xsd:element>

これを xjc.exe で実行すると、@XmlElement に対して次の注釈が生成されます

@XmlElements({
    @XmlElement(name = "HostQueryRq", type = HostQueryRqType.class),
    @XmlElement(name = "CompanyQueryRq", type = CompanyQueryRqType.class),
    @XmlElement(name = "CompanyActivityQueryRq", type = CompanyActivityQueryRqType.class),
    //+ et al
})
protected List<Object> hostQueryRqOrCompanyQueryRqOrCompanyActivityQueryRq;

では、この JAXB 構造を SimpleXML 注釈付きクラス構造に変換するにはどうすればよいでしょうか?

4

1 に答える 1

4

答えは、ElementListUnionを使用して、List タイプで使用可能な選択肢を識別することです。ここの「さまざまなタイプを 1 つのリストにまとめる」を確認してください。例:

@Root
public class Example {

   @ElementListUnion({
      @ElementList(entry="int", type=Integer.class, inline=true),
      @ElementList(entry="date", type=Date.class, inline=true),
      @ElementList(entry="text", type=String.class, inline=true)
   })
   private List<Object> list;
}
于 2012-12-04T17:03:17.520 に答える