如果类具有@XmlElement属性,则它不能具有@XmlValue属性 [英] If a class has @XmlElement property, it cannot have @XmlValue property
问题描述
我收到以下错误:
If a class has @XmlElement property, it cannot have @XmlValue property
已更新类:
@XmlType(propOrder={"currencyCode", "amount"})
@XmlRootElement(name="priceInclVat")
@XmlAccessorType(XmlAccessType.FIELD)
public class PriceInclVatInfo {
@XmlAttribute
private String currency;
@XmlValue
private String currencyCode;
private double amount;
public PriceInclVatInfo() {}
public PriceInclVatInfo(String currency, String currencyCode, double amount) {
this.currency = currency;
this.currencyCode = currencyCode;
this.amount = amount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
我想获得以下输出,具有元素属性和值:
I want to achive the following output, with an element attribute and value:
<currencyCode plaintext="£">GBP</currencyCode>
我如何实现这一目标?是否可以使用@XmlRootElement(name =priceInclVat)?
How can I achieve this? Is it possible if I have @XmlRootElement(name="priceInclVat")?
推荐答案
对于错误:
如果某个类具有@XmlElement属性,则它不能具有@XmlValue属性
If a class has @XmlElement property, it cannot have @XmlValue property
由于您已指定了字段访问权限,因此默认情况下,未注释的金额
字段被视为具有 @XmlElement
。
Since you have specified field access, by default the unannotated amount
field is treated as having @XmlElement
.
private double amount;
您可以执行以下操作之一:
You can do one of the following:
- 使用
@XmlAttribute
- 金额 >使用
@XmlTransient
注释金额
。 - 更改
@XmlAccessorType(XmlAccessType.FIELD)
到@XmlAccessorType(XmlAccessType.NONE)
,以便只将带注释的字段视为已映射。
- Annotate
amount
with@XmlAttribute
- Annotate
amount
with@XmlTransient
. - Change
@XmlAccessorType(XmlAccessType.FIELD)
to@XmlAccessorType(XmlAccessType.NONE)
so that only annotated fields are treated as mapped.
我如何实现这一目标?是否有可能我有
@XmlRootElement(name =priceInclVat)?
How can I achieve this? Is it possible if I have @XmlRootElement(name="priceInclVat")?
你可以包装<的实例code> PriceInclVatInfo 在 JAXBElement
的实例中覆盖根元素并编组该元素。
You can wrap the instance of PriceInclVatInfo
in an instance of JAXBElement
to override the root element and marshal that.
这篇关于如果类具有@XmlElement属性,则它不能具有@XmlValue属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!