Customize a property by using other property values while Marshalling through Jaxb

时间:2015-10-30 21:54:08

标签: java xml jaxb

For Example I have a sample Java object and want to marshal

@XmlElement(name = "itemName")
private String name;

@XmlElement(name = "quantity")
private int quantity;

@XmlElement(name = "Price")
private String price;

@XmlElement(name = "totalPrice")
private String total;

Here I want marshal such a way that multiply quantity with Price and want to assign that value to totalPrice

How can we achieve this using Jaxb? Can anyone help on this

1 个答案:

答案 0 :(得分:0)

通过添加

解决了这个问题
   @XmlElement(name = "totalPrice")
   private String getTotal(){
        return String.valueOf(Integer.parseInt(this.getPrice()) + this.getQuantity());
   }

并删除

@XmlElement(name = "totalPrice")
private String total;