有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何向xsi:nil元素添加另一个属性?

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

具有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.“template”,它不会将xsi:nil属性设置为true。若我将null对象添加到列表中,我将得到xsi:nil=“true”,但我无法设置nilReason值

我试图寻找一些绑定方法,但没有成功

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

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

但我确实喜欢避免干扰生成的类

有什么想法吗


共 (0) 个答案