I get the following error:
If a class has @XmlElement property, it cannot have @XmlValue property
updated class:
@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>
How can I achieve this? Is it possible if I have @XmlRootElement(name="priceInclVat")?
For the error:
If a class has @XmlElement property, it cannot have @XmlValue property
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:
amount
with @XmlAttribute
amount
with @XmlTransient
.@XmlAccessorType(XmlAccessType.FIELD)
to @XmlAccessorType(XmlAccessType.NONE)
so that only annotated fields are treated as mapped.How can I achieve this? Is it possible if I have @XmlRootElement(name="priceInclVat")?
You can wrap the instance of PriceInclVatInfo
in an instance of JAXBElement
to override the root element and marshal that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With