Xstream序列化实体

Java序列化的日期为是很标准,XStream中转换为标准的做法

 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import com.thoughtworks.xstream.converters.ConversionException;
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;

public class DateConverter implements Converter {
 private Locale locale;

 public DateConverter(Locale locale) {
  super();
  this.locale = locale;
 }

 @Override
 public void marshal(Object value, HierarchicalStreamWriter writer,
   MarshallingContext context) {
  Date date = (Date) value;
  DateFormat formatter = new SimpleDateFormat(
    "yyyy-MM-dd'T'HH:mm:ss.SSS");
  // DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL,
  // this.locale);
  // 在这写输出的格式
  writer.setValue(formatter.format(date));
 }

 @Override
 public Object unmarshal(HierarchicalStreamReader reader,
   UnmarshallingContext context) {
  GregorianCalendar calendar = new GregorianCalendar();
  DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL,
    this.locale);
  try {
   calendar.setTime(formatter.parse(reader.getValue()));
  } catch (ParseException e) {
   throw new ConversionException(e.getMessage(), e);
  }
  return calendar;
 }

 @Override
 public boolean canConvert(Class clazz) {
  // return Calendar.class.isAssignableFrom(clazz);
  return Date.class.isAssignableFrom(clazz);
 }

}

 

去掉Java的包名等

 

package com.anson.ws;

//import java.io.ByteArrayOutputStream;

import java.io.ByteArrayOutputStream;
import java.util.Locale;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author jeo 08/12/29
 */
public class MQObjectToXMLUtil {

 private static XStream sxstream;
 static {
  sxstream = new XStream(new DomDriver());

  // 把MQ所用到的类的完全限定名,改为类名
  sxstream.alias("Person", Person.class);
  sxstream.alias("Book", Book.class);
  
  //加入日期转换器
  DateConverter converter = new DateConverter(Locale.CHINESE);
  sxstream.registerConverter(converter);
 }

 /**
  * 把java的可序列化的对象转换为XML格式的字符串
  *
  * @param obj
  *            要序列化的可序列化的对象
  */
 public static String objectXmlEncoder(Object obj) {
  ByteArrayOutputStream bout = new ByteArrayOutputStream();

  // XMLEncoder encoder = new XMLEncoder(bout);
  String xml = null;
  try {
   // 对象序列化输出到字节流
   // encoder.writeObject(obj);
   // encoder.flush();
   // 关闭序列化工具
   // encoder.close();
   // 由字节流中取得XML格式字符串

   sxstream.toXML(obj, bout);
   xml = new String(bout.toByteArray(), "utf-8");
   // 关闭字节流
   // bout.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return xml;
 }
}

 

转载于:https://www.cnblogs.com/Yjianyong/archive/2012/03/01/2375042.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值