我有一个模式,即a schema for metalink file。为此我想使用Maven Maven-jaxb2-plugin生成类。JAXB,如何为具有元素重复属性的模式生成类
该模式允许以属性或元素的形式输入版本,因此它具有重复的属性。我如何使用Jaxb为这样的模式生成一个Java类(甚至是可能的),而不需要重命名属性或元素。所以基本上我想这个重复的属性映射到某种复杂类型与内部指示是否属性或元素。至少这样的解决方案会把我看作XML的最接近的表示。
可以这样做吗?如果是,那么如何?
下面,说有问题的xsd片段。
<xs:complexType name="metalinkType">
<xs:all>
<xs:element minOccurs="0" name="description" type="xs:string"/>
<xs:element minOccurs="0" name="identity" type="xs:string"/>
<xs:element minOccurs="0" name="license" type="licenseType"/>
<xs:element minOccurs="0" name="publisher" type="publisherType"/>
<xs:element minOccurs="0" name="releasedate" type="RFC822DateTime"/>
<xs:element minOccurs="0" name="tags" type="xs:string"/>
<xs:element minOccurs="0" name="version" type="xs:string"/>
<xs:element name="files" type="filesType"/>
</xs:all>
<xs:attribute name="version" type="xs:string" default="3.0"/>
<xs:attribute name="origin" type="xs:anyURI"/>
<xs:attribute name="type" default="static">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="dynamic"/>
<xs:enumeration value="static"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="pubdate" type="RFC822DateTime"/>
<xs:attribute name="refreshdate" type="RFC822DateTime"/>
<xs:attribute name="generator" type="xs:string"/>
</xs:complexType>
编辑:
而对于这样的模式,我想已经由JAXB生成的Java模型,如:
public class Metalink {
private PropertyType<String> version;
...
}
public class PropertyType<T> {
private T val;
private boolean isAttribute;
...
}
是否有可能实现这样的事情或者是在Java模型中有两个不同属性的唯一解决方案?
你可以使用'@ XmlJavaTypeAdapter'来创建一个你想要的结构,但你总是会以一个基于两个属性的Jaxb Java模型结束....我建议采取我的方法。 –