首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

XStream - A simple library to serialize objects to XML and back again

2012-07-19 
XStream - A simple library to serialize objects to XML and back again.官网:http://xstream.codehaus.

XStream - A simple library to serialize objects to XML and back again.
官网:http://xstream.codehaus.org/index.html

使用中需要注意的问题:
1.java对象的字段如果有下划线xstream转成xml时会变成双下划线,解决办法:



package com.iteye.am-human.message.msgDefinition;import java.util.Date;import java.text.ParseException;import java.text.SimpleDateFormat;import com.thoughtworks.xstream.converters.Converter;import com.thoughtworks.xstream.converters.MarshallingContext;import com.thoughtworks.xstream.converters.UnmarshallingContext;import com.thoughtworks.xstream.io.HierarchicalStreamReader;import com.thoughtworks.xstream.io.HierarchicalStreamWriter;/** * Date数据转换器 * 默认的format格式为:yyyyMMddHHmmss *  * @author Wuyc * */public class DateSingleValueConverter implements Converter {private SimpleDateFormat sdf;public DateSingleValueConverter(){this.sdf = new SimpleDateFormat("yyyyMMddHHmmss");}public DateSingleValueConverter(String formatString){this.sdf = new SimpleDateFormat(formatString); }public void marshal(Object arg0, HierarchicalStreamWriter writer,MarshallingContext arg2) {Date date = (Date)arg0;writer.setValue(sdf.format(date));}public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext arg1) {Date date = null;try {String strDate = reader.getValue();if(strDate != null && strDate.length()!=0 && !"".equals(strDate) && !"0".equals(strDate)){date = sdf.parse(reader.getValue());}} catch (ParseException e) {e.printStackTrace();}return date;}public boolean canConvert(Class type) {return Date.class.equals(type);}}


其他的xml转换框架:
castor
官网:http://www.castor.org/
通过定义mapping文件,实现xml和javabean的对应。

JOX
官网:http://www.wutka.com/jox.html
sourceforge(官网打不开):http://sourceforge.net/projects/jox/

热点排行
Bad Request.