Domanda

I'm using xjc with no arguments on the following schema:

<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>

It produces:

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. Why is xjc choosing to generate an instance variable of UserType instead of something like List when I haven't specified a maxOccurs in my addedUsersType xsd definition?
  2. Is there a way to force xjc generate list structures when it encounters definitions with no maxOccurs specified?
È stato utile?

Soluzione

When no maxOccurs is specified the default value is 1. This is why the property is not generated as a List.

To get a List you need to specify the maxOccurs attribute on the element definition with a value of at least 2:

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

or unbounded:

<xs:element name="user" type="abc:userType" maxOccurs="unbounded"/>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top