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 类型的可用选项。在“在单个列表中收集各种类型”下查看此处。例子:

@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 回答