如果类具有@XmlElement属性,则它不能具有@XmlValue属性 [英] If a class has @XmlElement property, it cannot have @XmlValue property

查看:689
本文介绍了如果类具有@XmlElement属性,则它不能具有@XmlValue属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

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:


  1. 使用 @XmlAttribute

  2. 金额 >使用 @XmlTransient 注释金额
  3. 更改 @XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.NONE),以便只将带注释的字段视为已映射。

  1. Annotate amount with @XmlAttribute
  2. Annotate amount with @XmlTransient.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆