2012-12-03 33 views
3

我试图将一些JAXB xjc.exe生成的类转换为简单的XML类。我不知道如何注释动态元素。例如,在架构中,我有:多类型列表注释转换:从JAXB到SimpleXML

<!-- 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

答案是使用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; 
}