我通过编组从 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。如果我将空对象添加到列表中,我将得到 xsi:nil="true" 但我无法设置 nilReason 值。
我试图寻找一些绑定方法,但没有运气。
我发现的唯一解决方案是手动将属性添加到生成的类中,如下所示:
@XmlAttribute(namespace = "http://www.w3.org/2001/XMLSchema-instance")
private boolean nil;
但我真的很想避免干扰生成的类。
有任何想法吗?