6

我们有一个带有如下声明的 xsd 模式:

<xsd:simpleType name="customId">
    <xsd:annotation>
        <xsd:appinfo>
            <jaxb:javaType name="com.company.identifiers.CustomId" parseMethod="fromString" printMethod="toString"/>
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:restriction base="xsd:int" />
</xsd:simpleType>

然后,我想在生成的 Java 类中有一个这种类型的列表:

<xsd:complexType name="SomeMessage">
    ...
<xsd:attribute name="customIds" use="optional">
        <xsd:simpleType>
            <xsd:list itemType="customId" />
        </xsd:simpleType>
</xsd:attribute>
    ...
</xsd:complexType>

但是由于customIds某种原因,该字段生成为List<String>

我想,xsd:sequence可以用来代替xsd:list,但SomeMessage已经有一个xsd:choice,据我所知,xsd:sequence在同一个声明中是非法的。

谢谢!

4

1 回答 1

3

使用 NetBeans 7.1.2 生成的代码,在 Java 1.7.0_02 上运行。

如果要将简单类型映射到 Java 类,一种方法是全局设置mapSimpleTypeDef="true"

<xsd:annotation>
    <xsd:appinfo>
        <jaxb:globalBindings mapSimpleTypeDef="true"/>
    </xsd:appinfo>
</xsd:annotation>

生成的代码:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeMessage")
public class SomeMessage {

    @XmlAttribute(name = "customIds")
    protected List<CustomId> customIds;

    /**
     * Gets the value of the customIds property.
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link CustomId }
     * 
     * 
     */
    public List<CustomId> getCustomIds() {
        if (customIds == null) {
            customIds = new ArrayList<CustomId>();
        }
        return this.customIds;
    }

}

如果您想引用预先存在的 CustomId 类,那么在我的情况下可以使用以下方法:

<xsd:simpleType name="customId">
    <xsd:restriction base="xsd:int"/>
</xsd:simpleType>
<xsd:complexType name="SomeMessage">
    <xsd:attribute name="customIds" use="optional">
        <xsd:simpleType>
            <xsd:annotation>
                <xsd:appinfo>
                    <jaxb:javaType name="java.util.List&lt;com.company.identifiers.CustomId>" parseMethod="Class1.fromString" printMethod="Class1.toString"/>
                </xsd:appinfo>
            </xsd:annotation>
            <xsd:list itemType="customId"/>
        </xsd:simpleType>
    </xsd:attribute>
</xsd:complexType>

您将获得以下信息:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeMessage")
public class SomeMessage {

    @XmlAttribute(name = "customIds")
    @XmlJavaTypeAdapter(Adapter1 .class)
    protected List<CustomId> customIds;

    /**
     * Gets the value of the customIds property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public List<CustomId> getCustomIds() {
        return customIds;
    }

    /**
     * Sets the value of the customIds property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCustomIds(List<CustomId> value) {
        this.customIds = value;
    }

}

以及生成的Adapter1:

public class Adapter1
    extends XmlAdapter<String, List<CustomId>>
{


    public List<CustomId> unmarshal(String value) {
        return (Class1.fromString(value));
    }

    public String marshal(List<CustomId> value) {
        return (Class1.toString(value));
    }

}
于 2012-09-24T14:02:01.583 回答