JAXB,如何为具有元素重复属性的模式生成类

时间:2016-08-05 11:43:55

标签: java xsd jaxb

我有一个架构,即a schema for metalink file。我想要使​​用Maven maven-jaxb2-plugin生成类。

此架构允许将版本作为属性或元素输入,因此它具有重复的属性。如何使用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模型中拥有两个不同属性的唯一解决方案?

1 个答案:

答案 0 :(得分:1)

是的,可以通过JAXB Binding Customization完成。

这是我为解决您的问题而做的一个小XSD(属性和元素之间的名称冲突):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://hello"
    elementFormDefault="qualified"
    xmlns:tns="http://hello"
>
    <xs:element name="metalink" type="tns:metalinkType" />
    <xs:complexType name="metalinkType">
        <xs:sequence>
            <xs:element name="version" type="xs:string" minOccurs="1" />
        </xs:sequence>
        <xs:attribute name="version" type="xs:string" use="required" />
    </xs:complexType>
</xs:schema>

eclipse 执行JAXB生成时,出现如下错误:

[ERROR] Property "Version" is already defined. Use <jaxb:property> to resolve this conflict

但是,当我使用一些自定义标签修改XSD时,问题就消失了:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://hello"
    elementFormDefault="qualified"
    xmlns:tns="http://hello"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" <!-- ADDED -->
    jaxb:version="1.0" <!-- ADDED -->
>
    <xs:element name="metalink" type="tns:metalinkType" />
    <xs:complexType name="metalinkType">
        <xs:sequence>
            <xs:element name="version" type="xs:string" minOccurs="1" />
        </xs:sequence>
        <xs:attribute name="version" type="xs:string" use="required" >
            <xs:annotation>
                <xs:appinfo>
                    <jaxb:property name="version2"/> <!-- ADDED! -->
                </xs:appinfo>
            </xs:annotation>
        </xs:attribute>
    </xs:complexType>
</xs:schema>

避免碰撞问题并允许生成相关的类。

注意:此示例假设您希望(并且您可以)使用内联自定义修改有问题的XSD ...根据文档,您还可以在外部文件中定义自定义项(您需要检查并试试这个)。

this是另一个感兴趣的链接...

希望这有帮助!

**更新**:实现您想要的Java模型的示例..该示例假定您遵循之前描述的步骤...您将以这样的类结束:

// xml annotations
public class MetalinkType {
     ...
    @XmlElement(name = "version)
    private String version;
    ...
    @XmlAttribute(name = "version")
    private String version2;
    ...
    public void setVersion(String version, boolean asElem) {
        If (asElem) {
            this.version = version;
        } else {
            this.version2 = version;
        }
    }
    // individual setter as setVersion and setVersion2 are not exposed
}
相关问题