如何将另一个属性添加到xsi:nil元素?

时间:2012-12-04 09:13:22

标签: java xml jaxb gml xml-nil

我通过编组从xsd生成的类来生成xml(gml)时遇到问题(我正在使用maven2 xjc插件)。

具有gml:nilReason属性的元素出现问题,如下所示:

               <element name="foo" nillable="true" maxOccurs="unbounded">
                    <complexType>
                        <complexContent>
                            <extension base="string">
                                <attribute name="nilReason" type="gml:NilReasonType"/>
                            </extension>
                        </complexContent>
                    </complexType>
                </element>

生成的类看起来像这样:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Foo
    extends String
{

    @XmlAttribute
    protected List<String> nilReason;

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

    public boolean isSetNilReason() {
        return ((this.nilReason!= null)&&(!this.nilReason.isEmpty()));
    }

    public void unsetNilReason() {
        this.nilReason = null;
    }

    public boolean isNil() {
        return nil;
    }

    public void setNil(boolean nil) {
        this.nil = nil;
    }

}

问题是属性nilReason应该与xsi:nil一起设置。如果我创建Foo的实例并添加到nilReason列表值ex。 “模板”它不会将xsi:nil属性设置为true。如果我将null对象添加到列表中,我将获得xsi:nil =“true”,但我无法设置nilReason值。

我试图寻找一些绑定方法,但没有运气。

我找到的唯一解决方案是手动将属性添加到生成的类中,如下所示:

@XmlAttribute(namespace = "http://www.w3.org/2001/XMLSchema-instance")
    private boolean nil;

但我真的想避免干扰生成的类。

有什么想法吗?

0 个答案:

没有答案