我正在尝试将一些 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/