【问题标题】:multi-type List annotation translation: JAXB to SimpleXML多类型列表注释翻译:JAXB 到 SimpleXML
【发布时间】:2012-12-19 22:44:41
【问题描述】:

我正在尝试将一些 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 注释的类结构呢?

【问题讨论】:

    标签: java xml jaxb xsd simple-framework


    【解决方案1】:

    答案是使用ElementListUnion 来识别列表类型的可用选项。检查“在单个列表中收集各种类型”下的here。示例:

    @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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      相关资源
      最近更新 更多