java - xsd: 自定义类型的列表生成到 List<String>

标签 java xsd jaxb

我们有一个带有如下声明的 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 是非法的在同一声明中。

谢谢!

最佳答案

使用 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));
    }

}

关于java - xsd: 自定义类型的列表生成到 List<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564004/

相关文章:

java - XmlAdapter 在 JAXB RI 中没有按预期工作

java - JAXB 没有为包含生成的 Java 类的类生成所需的模式?

java - JaxB 验证事件定位器 - 错误的对象引用

java - FilteredItemsSelectionDialog - 刷新内容提供程序

java - Easymock 调用 Autowiring 的对象方法

java - 将 html 内容添加到 Liferay 中的页面

xml - 如何将特定的命名空间添加到我的 xsd

java - Tomcat7 maven 插件-处理请求时捕获 I/O 异常 (java.net.SocketException) : Broken pipe

c# - XSD 数据集与 Oracle 数据库

java - 以逗号作为小数分隔符的 Jaxb 数字转换