xjc在预期时不生成列表

时间:2014-04-26 00:17:41

标签: xml jaxb xsd xjc

我正在使用xjc而没有关于以下架构的参数:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:abc="http://someurl.com/schemas"
    elementFormDefault="qualified" 
    targetNamespace="http://someurl.com/schemas">
    <xs:element name="MyResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="addedUsers" type="abc:addedUsersType" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="addedUsersType">
        <xs:sequence>
            <xs:element name="user" type="abc:userType" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="userType">
        <xs:sequence>
            <xs:element name="emailAddress" type="xs:string" maxOccurs="1" minOccurs="1" />
            <xs:element name="successfullyAdded" type="xs:boolean" maxOccurs="1" minOccurs="1" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

它产生:

package com.someurl.schemas;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addedUsersType", propOrder = {
    "user"
})
public class AddedUsersType {

    @XmlElement(required = true)
    protected UserType user;

    public UserType getUser() {
        return user;
    }

    public void setUser(UserType value) {
        this.user = value;
    }
}
  1. 当我在addedUsersType xsd定义中未指定maxOccurs时,为什么xjc选择生成UserType的实例变量而不是List之类的东西?
  2. 当遇到没有指定maxOccurs的定义时,有没有办法强制xjc生成列表结构?

1 个答案:

答案 0 :(得分:3)

如果未指定maxOccurs,则默认值为1。这就是该属性不是作为List生成的原因。

要获得List,您需要在元素定义上指定maxOccurs属性,其值至少为2

<xs:element name="user" type="abc:userType" maxOccurs="2"/>

unbounded

<xs:element name="user" type="abc:userType" maxOccurs="unbounded"/>