DoubleAdapter.java

Download DoubleAdapter.java

 1: package com.wpollock.jaxbdemo;
 2: 
 3: import javax.xml.bind.annotation.adapters.XmlAdapter;
 4: import java.math.BigDecimal;
 5: 
 6: /**
 7:  * JAXB does native formatting of values.
 8:  * To have nicely formatted dates and numbers requires a XMLAdapter class
 9:  * be used.  This class formats BigDecimals with two decimal places.
10:  */
11: public class DoubleAdapter extends XmlAdapter<String, BigDecimal> {
12: 
13:     public BigDecimal unmarshal(final String xml) throws Exception {
14:         return new BigDecimal(xml);
15:     }
16: 
17:     public String marshal(final BigDecimal num) throws Exception {
18:         return String.format("%.2f", num);
19:     }
20: }