java-多类型列表注释转换:JAXB到SimpleXML
作者:互联网
我正在尝试将一些JAXB xjc.exe生成的类转换为Simple 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;
}
标签:jaxb,simple-framework,xsd,xml,java 来源: https://codeday.me/bug/20191031/1976679.html