java - 多类型List注解翻译: JAXB to SimpleXML

标签 java xml jaxb xsd simple-framework

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

最佳答案

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

关于java - 多类型List注解翻译: JAXB to SimpleXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13691292/

相关文章:

JavaFX ListView 和 ContextMenu - getSelectedItem() 返回 null

php - 如何执行PHPUnit?

java - 使用单个 JAXB 带注释的类解码复杂类型

java - JAXB2 Mtom 附件被 BOM 损坏

java - 什么是 "java.sql.SQLException: No value specified for parameter 1 "?

java - SwingWorker 并从套接字检索数据

javascript - 如何从以下 xml 制作层次结构数组?

jaxb - 如何在 Jersey(JAX-WS) 上自定义命名空间前缀

java - 无法反序列化对象。没有定义无参数构造函数。如果您使用 ProGuard,请确保这些构造函数没有被剥离

java - 短信应用程序 - List_item 错误