我目前有一个看起来像这样的 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 的列表。
我想要这些字段的单独设置器和获取器,而 childCategory 是一个列表,可以包含另一个 childCategory 列表以及它自己的 desc/name/version 等值。基本上,我希望从 XJC 自动生成显式 setter 和 getter。所有其他方法都失败了,我总是可以手动编写代码,但我想知道是否有一些技巧可以强制 XJC 按照我想要的方式生成代码。