If the class has the @XmlElement property, it cannot have the @XmlValue property

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 get the following output with attribute and element value:

<currencyCode plaintext="£">GBP</currencyCode>

How can i achieve this? Is it possible if I have @XmlRootElement (name = "priceInclVat")?

+4
source share
1 answer

For the error:

If the class has the @XmlElement property, it cannot have the @XmlValue property

Since you specified access to the field, by default, a human field is amounttreated as having @XmlElement.

private double amount;

You can do one of the following:

  • Annotate amountwith@XmlAttribute
  • Annotate amountwith @XmlTransient.
  • @XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.NONE) , .

? , @XmlRootElement (name= "priceInclVat" )?

PriceInclVatInfo JAXBElement, .

+12

All Articles