xsd定义complexType的列表

来源:互联网 发布:网络购物诈骗案例分析 编辑:程序博客网 时间:2024/04/28 03:39

假设有如下结构,node节点下面有一个叫clist的子节点,而子节点下面有一个complexType,它包含name和title两个属性,这里要作为列表返回的就是这个complexType,也就是clist就是保存complexType的List。

用xsd来描述:


<xs:element name="node">    <xs:complexType>        <xs:sequence>            <xs:element name = "clist" maxOccurs="unbounded">                <xs:complexType>                    <xs:sequence>                        <xs:element name="name" type="xs:string"/>                        <xs:element name="title" type="xs:string"/>                    </xs:sequence>                </xs:complexType>            </xs:element>        </xs:sequence>    </xs:complexType></xs:element>
生成列表比较关键的就是把maxOccurs属性设置为unbounded

使用jaxb2插件把xsd文件生成java代码(eclipse和idea都有)

上述文件生成的代码内容如下:

@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "", propOrder = {    "clist"})@XmlRootElement(name = "node")public class Node {    @XmlElement(required = true)    protected List<Node.Clist> clist;    /**     * Gets the value of the clist property.     *      * <p>     * This accessor method returns a reference to the live list,     * not a snapshot. Therefore any modification you make to the     * returned list will be present inside the JAXB object.     * This is why there is not a <CODE>set</CODE> method for the clist property.     *      * <p>     * For example, to add a new item, do as follows:     * <pre>     *    getClist().add(newItem);     * </pre>     *      *      * <p>     * Objects of the following type(s) are allowed in the list     * {@link Node.Clist }     *      *      */    public List<Node.Clist> getClist() {        if (clist == null) {            clist = new ArrayList<Node.Clist>();        }        return this.clist;    }    /**     * <p>anonymous complex type的 Java 类。     *      * <p>以下模式片段指定包含在此类中的预期内容。     *      * <pre>     * <complexType>     *   <complexContent>     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">     *       <sequence>     *         <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>     *         <element name="title" type="{http://www.w3.org/2001/XMLSchema}string"/>     *       </sequence>     *     </restriction>     *   </complexContent>     * </complexType>     * </pre>     *      *      */    @XmlAccessorType(XmlAccessType.FIELD)    @XmlType(name = "", propOrder = {        "name",        "title"    })    public static class Clist {        @XmlElement(required = true)        protected String name;        @XmlElement(required = true)        protected String title;        /**         * 获取name属性的值。         *          * @return         *     possible object is         *     {@link String }         *              */        public String getName() {            return name;        }        /**         * 设置name属性的值。         *          * @param value         *     allowed object is         *     {@link String }         *              */        public void setName(String value) {            this.name = value;        }        /**         * 获取title属性的值。         *          * @return         *     possible object is         *     {@link String }         *              */        public String getTitle() {            return title;        }        /**         * 设置title属性的值。         *          * @param value         *     allowed object is         *     {@link String }         *              */        public void setTitle(String value) {            this.title = value;        }    }}

这时我们就可以看到设置了maxOccurs属性为unbounded的clist是作为List定义的。

原创粉丝点击