javajaxb格式化_XStream、JAXB⽇期(Date)、数字
(Number)格式。。。
XStream、Jaxb是java中⽤于对象xml序列化/反序列化 的经典开源项⽬,利⽤它们将对象转换成xml时,经常会遇到⽇期(Date)、数字按指定格式输出的需求,下⾯是使⽤⽰例:
⼀、⽇期字段格式化输出
1.1 xStream
1 XStream x = newXStream();
isterConverter(new DateConverter("yyyy-MM-dd HH:mm:ss",
TimeZone("GMT+8")));
xStream默认使⽤UTC时间格式输出,上⾯的代码演⽰了如何按北京时间输出 yyyy-MM-dd HH:mm:ss 格式
1.2 jaxb
jaxb处理这个要⿇烦⼀点,先要创建⼀个Adapter,下⾯是⽰例
1 st;2
DateFormat;
SimpleDateFormat;
5 importjava.util.Date;6
l.bind.annotation.adapters.XmlAdapter;8
9 public class JaxbDateAdapter extends XmlAdapter{10 static final String STANDARM_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";11
12 @Override13 public Date unmarshal(String v) throwsException {14 if (v == null) {15 return null;16 }17
18 DateFormat format = newSimpleDateFormat(STANDARM_DATE_FORMAT);19 returnformat.parse(v);20 }21
22 @Override23 public String marshal(Date v) throwsException {24 DateFormat format =
newSimpleDateFormat(STANDARM_DATE_FORMAT);25 returnformat.format(v);26 }27 }
然后要处理的dto类,相应的Date字段的get⽅法上使⽤刚才这个Adapter
@XmlJavaTypeAdapter(JaxbDateAdapter.class)publicDate getCreateDate() {returncreateDate;
}
注:不要在private上使⽤,最好将注解打在get⽅法上,否则有可能报错。
这⾥,再给⼀个List类型的常见⽤法:
@XmlElementWrapper(name="details")
@XmlElement(name="detail")
public List getDetails() {
return details;
}
如果没有这⼆个注解,xml的结果类似:
...
...
...
...
加上这⼆个注释后,xml的结果类似:
...
...
...
...
⼆、数字格式化
假设我们要将⼀个Double型的成员,按中国货币的格式输出
2.1 xStream
默认的DoubleConverter满⾜不了要求,得从它派⽣⼀个⼦类来重写toString(Object obj)⽅法
1 st;2
NumberFormat;
4 importjava.util.Locale;5
6 importcom.verters.basic.DoubleConverter;7
8 public class DoubleToCurrencyStringConverter extendsDoubleConverter {9
10 NumberFormat format;11
12 publicDoubleToCurrencyStringConverter(Locale local) {13 format =CurrencyInstance(local);14 }15
16 publicString toString(Object obj) {17 returnformat.format(obj);18 }19
20 }
然后这样使⽤:
1 XStream x = newXStream();
isterConverter(new DoubleToCurrencyStringConverter(Locale.CHINA));
2.2 Jaxb
仍然是按Adapter的⽼路,定义⼀个专⽤的Adapter
1 st;2
NumberFormat;
4 importjava.util.Locale;5
l.bind.annotation.adapters.XmlAdapter;7
java xml是什么
8 public class JaxbNumberAdapter extends XmlAdapter{9
10 @Override11 public Number unmarshal(String v) throwsException {12 if (v == null) {13 return null;14 }15 NumberFormat format =CurrencyInstance(Locale.CHINA);16 returnformat.parse(v);17 }18
19 @Override20 public String marshal(Number v) throwsException {21 NumberFormat format
=CurrencyInstance(Locale.CHINA);22 returnformat.format(v);23 }24 }
然后在相关的Double字段的get⽅法上,⽤注解使⽤这个Adapter
1 @XmlJavaTypeAdapter(JaxbNumberAdapter.class)
2 publicDouble getAmount() {
3 returnamount;
4 }
最后附⼀个完整的⽰例:
为演⽰效果,先定义⼀个Dto类:
1 st;2
3 importjava.io.Serializable;
4 importjava.util.Date;5
l.bind.annotation.XmlRootElement;
l.bind.annotation.adapters.XmlJavaTypeAdapter;
8 importcom.thoughtworks.xstream.annotations.XStreamAlias;9
10 @XmlRootElement(name = "sample")11 @XStreamAlias("sample")12 public class Sample implementsSerializable{13
14 private static final long serialVersionUID = -6271703229325404123L;15
16 privateDouble amount;17
18
19 privateDate createDate;20
21 @XmlJavaTypeAdapter(JaxbNumberAdapter.class)22 publicDouble getAmount() {23 returnamount;24 }25
26 public voidsetAmount(Double amount) {27 this.amount =amount;28 }29
30 @XmlJavaTypeAdapter(JaxbDateAdapter.class)31 publicDate getCreateDate() {32 returncreateDate;33 }34
35 public voidsetCreateDate(Date createDate) {ateDate =createDate;37 }38
39 }
同时为了使⽤jaxb更⽅便,定义⼀个JaxbUtil辅助类
1 packagecomblogs.yjmyzz.util;2
3 importjava.io.StringReader;
4 importjava.io.StringWriter;5
l.bind.JAXBContext;
l.bind.Marshaller;
l.bind.Unmarshaller;9
10 public classJaxbUtil {11
12 public staticString toXml(Object obj) {13 return toXml(obj, "UTF-8", false);14 }15
16 public staticString toXml(Object obj, String encoding,17 booleanisFormatOutput) {18 String result = null;19 try{20 JAXBContext context =Class());21 Marshaller marshaller =ateMarshaller();22 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,23 isFormatOutput);24
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);25
26 StringWriter writer = newStringWriter();27 marshaller.marshal(obj, writer);28 result =String();29 }
catch(Exception e) {30 e.printStackTrace();31 }32
33 returnresult;34 }35
36 @SuppressWarnings("unchecked")37 public static T toObject(String xml, Classc) {38 T t = null;39 try{40 JAXBContext context =wInstance(c);41 Unmarshaller unmarshaller =ateUnmarshaller();42 t = (T) unmarshaller.unmarshal(newStringReader(xml));43 } catch(Exception e) {44 e.printStackTrace();45 }46
47 returnt;48 }49 }
完整的单元测试如下:
1 st;2
3 importjava.util.Calendar;
4 importjava.util.Locale;
5 importjava.util.TimeZone;
6 importorg.junit.Test;7
importcomblogs.yjmyzz.util.JaxbUtil;8 importcom.verters.basic.DateConverter;9
importcom.thoughtworks.xstream.XStream;10
11 public classXStreamAndJaxbTest {12
13 privateSample getSampleObj() {14 Calendar c =Instance(Locale.CHINA);15 c.set(2014, 9, 31, 0, 0, 0);16 Sample obj = newSample();17 obj.Time());18 obj.setAmount(1234.5678);19 returnobj;20 }21
22 @Test23 public voidtestXStream() {24 XStream x = newXStream();25 x.alias("sample", Sample.class);26
34 @Test35 public voidtestJaxb() {36 System.out.println("==> jaxb ==>\n");37
System.out.Xml(getSampleObj(),"UTF-8",true));38 System.out.println("\n\n");39 }40
41 }
测试结果如下:
==> jaxb ==>
¥1,234.57
2014-10-31 00:00:00
==> xstream ==>
¥1,234.57
2014-10-31 00:00:00

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。