Java XSD代码生成问题

时间:2012-10-12 21:38:23

标签: java xsd xjc

我目前有一个看起来像这样的XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="apiCategoryResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="category"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="category">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="version"/>
        <xs:element ref="name"/>
        <xs:element ref="desc"/>
        <xs:element ref="id"/>
        <xs:element maxOccurs="unbounded" ref="subCategory"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="subCategory">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="childCategory"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="version" type="xs:integer"/>
  <xs:element name="name" type="xs:NCName"/>
  <xs:element name="desc" type="xs:NCName"/>
  <xs:element name="id" type="xs:integer"/>
  <xs:element name="childCategory">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="childCategory"/>
        <xs:element ref="desc"/>
        <xs:element ref="id"/>
        <xs:element ref="name"/>
        <xs:element ref="version"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

当我使用XJC生成Java代码时,ChildCategory会生成类似这样的内容,

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "childCategoryOrDescOrId"
})
@XmlRootElement(name = "childCategory")
public class ChildCategory {

    @XmlElementRefs({
        @XmlElementRef(name = "name", type = JAXBElement.class),
        @XmlElementRef(name = "id", type = JAXBElement.class),
        @XmlElementRef(name = "version", type = JAXBElement.class),
        @XmlElementRef(name = "childCategory", type = ChildCategory.class),
        @XmlElementRef(name = "desc", type = JAXBElement.class)
    })
    protected List<Object> childCategoryOrDescOrId;

    /**
     * Gets the value of the childCategoryOrDescOrId 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 childCategoryOrDescOrId property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getChildCategoryOrDescOrId().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
     * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link ChildCategory }
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * 
     * 
     */
    public List<Object> getChildCategoryOrDescOrId() {
        if (childCategoryOrDescOrId == null) {
            childCategoryOrDescOrId = new ArrayList<Object>();
        }
        return this.childCategoryOrDescOrId;
    }

}

问题是我不希望它是desc / id / name / etc的列表。

我希望这些字段有单独的setter和getter,而childCategory是一个列表,可以包含另一个childCategory列表以及它自己的desc / name / version等值。基本上我想要从XJC自动生成显式的setter和getter。所有其他失败我总是可以手动编码它,但我想知道是否有一些技巧迫使XJC以我想要的方式生成代码。

1 个答案:

答案 0 :(得分:2)

为什么不对category

做同样的事情
<xs:element name="childCategory">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="desc" />
      <xs:element ref="id" />
      <xs:element ref="name" />
      <xs:element ref="version" />
      <xs:element ref="childCategory" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>
相关问题