JaxB只为多个元素创建一个get和setter

时间:2015-02-04 12:59:20

标签: java xml xsd jaxb

我是JaxB和XML模式的新手。我一直在浏览谷歌,在这里找出为什么当我生成我的JaxB类时,它只为多个元素创建一个getter和一个setter。

例如,

公共类PolicyType {

@XmlElements({
    @XmlElement(name = "description", type = String.class),
    @XmlElement(name = "threshold", type = ThresholdType.class),
    @XmlElement(name = "boolean", type = BooleanType.class),
    @XmlElement(name = "whitelist", type = WhitelistType.class),
    @XmlElement(name = "policy", type = PolicyType.class),
    @XmlElement(name = "dirtylist", type = PolicyType.Dirtylist.class),
    @XmlElement(name = "cleanlist", type = PolicyType.Cleanlist.class),
    @XmlElement(name = "whiteImageList", type = PolicyType.WhiteImageList.class)
})
protected List<Object> descriptionOrThresholdOrBoolean;
@XmlAttribute(name = "name", required = true)
protected String name;

/**
 * Gets the value of the descriptionOrThresholdOrBoolean 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 descriptionOrThresholdOrBoolean property.
 * 
 * <p>
 * For example, to add a new item, do as follows:
 * <pre>
 *    getDescriptionOrThresholdOrBoolean().add(newItem);
 * </pre>
 * 
 * 
 * <p>
 * Objects of the following type(s) are allowed in the list
 * {@link String }
 * {@link ThresholdType }
 * {@link BooleanType }
 * {@link WhitelistType }
 * {@link PolicyType }
 * {@link PolicyType.Dirtylist }
 * {@link PolicyType.Cleanlist }
 * {@link PolicyType.WhiteImageList }
 * 
 * 
 */
public List<Object> getDescriptionOrThresholdOrBoolean() {
    if (descriptionOrThresholdOrBoolean == null) {
        descriptionOrThresholdOrBoolean = new ArrayList<Object>();
    }
    return this.descriptionOrThresholdOrBoolean;
}

每个元素都是它自己的类和我自己的xml中定义的类型......这是一个片段

    <xs:complexType name="policy.type">
    <xs:sequence maxOccurs="unbounded" minOccurs="0">
        <xs:element name="description" type="description.type" />
        <xs:element name="threshold" type="threshold.type" />
        <xs:element name="boolean" type="boolean.type" />
        <xs:element name="whitelist" type="whitelist.type" />
        <!--<xs:element name="policy" type="policy.type" />-->
        <xs:element name="dirtylist">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:complexType>
        </xs:element>
        <xs:element name="cleanlist">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:complexType>
        </xs:element>       
        <xs:element name="whiteImageList">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:complexType>
        </xs:element>
    </xs:sequence>
    <xs:attributeGroup ref="policy.attlist" />
</xs:complexType>

<xs:complexType name="policies.type">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="policy" type="policy.type" />
        <xs:element name="dirtylist" type="dirtylist.type" />
        <xs:element name="cleanlist" type="cleanlist.type" />
        <xs:element name="whiteImageList" type="whiteImageList.type" />
    </xs:sequence>
</xs:complexType>

<xs:element name="policies" type="policies.type" />

我试图让JaxB为每个定义的类型生成一个列表,而不是包含所有类型的对象列表。 (在解组之后检索我的所有信息会很麻烦)

思考?

如果您需要更多代码段,请告诉我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法,我想我会在这里发布。

通过将complexTypes policies.type和policy.type包装在这样的包装器中

    <xs:complexType name="policy">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="policy" type="policy.type"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="policy.type">
    <xs:choice>
        <xs:element name="description" type="description.type"/>
        <xs:element name="threshold" type="threshold.type"/>
        <xs:element name="boolean" type="boolean.type"/>
        <xs:element name="whitelist" type="whitelist.type"/>
        <xs:element name="dirtylist">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:complexType>
        </xs:element>
        <xs:element name="cleanlist">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:complexType>
        </xs:element>       
        <xs:element name="whiteImageList">
            <xs:complexType>
                <xs:attribute name="name" type="xs:string" use="required" />
            </xs:complexType>
        </xs:element>
    </xs:choice>
    <xs:attributeGroup ref="policy.attlist" />
</xs:complexType>

<xs:complexType name="policies">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="policies" type="policies.type"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="policies.type">
    <xs:choice>
        <xs:element name="policy" type="policy" />
        <xs:element name="dirtylist" type="dirtylist.type" />
        <xs:element name="cleanlist" type="cleanlist.type" />
        <xs:element name="whiteImageList" type="whiteImageList.type" />
    </xs:choice>
</xs:complexType>

<xs:element name="policies" type="policies" />

通过将包含多个元素的复杂类型包含在序列标记中包含单个元素的复杂类型,提示jaxb为这些列表创建多个getter和setter,而不是为整个组创建单个“list”。