【问题标题】:xjc Not Producing List When Expectedxjc 未按预期生成列表
【发布时间】:2014-04-26 00:17:40
【问题描述】:

我在以下架构上使用不带参数的 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 生成列表结构?

【问题讨论】:

    标签: xml jaxb xsd xjc


    【解决方案1】:

    当没有指定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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-16
      • 2020-06-23
      • 1970-01-01
      • 2018-10-13
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多