【发布时间】:2015-10-09 06:28:19
【问题描述】:
默认情况下,JAXB 似乎无法设置固定的属性值。这是预期的行为,还是我做错了什么?
我有一个 xsd 喜欢:
<element name="AccountCategory" type="tns:Integer"></element>
<xs:complexType name="Integer">
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute name="e-dtype" fixed="int"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
编组使用 new 生成的 java 对象:
<AccountCategory>5</AccountCategory>
Java:
com.sample.Integer val = new com.sample.Integer();
val.setValue(5);
parentObject.setAccountCategory(val);
我可以手动设置属性值,它工作正常。此外,如果我只是将其重置为自己的值,它也可以工作。好像 marshaller 在生成 XML 时没有使用 get 方法?
val.setEDtype(val.getEDtype());
结果
<AccountCategory e-dtype="int">5</AccountCategory>
生成的.java如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Integer", propOrder = {
"value"
})
public class Integer {
@XmlValue
protected int value;
@XmlAttribute(name = "e-dtype")
@XmlSchemaType(name = "anySimpleType")
protected String eDtype;
/**
* Gets the value of the value property.
*
*/
public int getValue() {
return value;
}
/**
* Sets the value of the value property.
*
*/
public void setValue(int value) {
this.value = value;
}
/**
* Gets the value of the eDtype property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getEDtype() {
if (eDtype == null) {
return "int";
} else {
return eDtype;
}
}
/**
* Sets the value of the eDtype property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setEDtype(String value) {
this.eDtype = value;
}
【问题讨论】: