• 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;
     }
    }

  • 相关阅读:
    (转)Hibernate框架基础——在Hibernate中java对象的状态
    (转)Hibernate框架基础——cascade属性
    (转)Hibernate框架基础——多对多关联关系映射
    (转)Hibernate框架基础——一对多关联关系映射
    (转)Hibernate框架基础——映射集合属性
    (转)Hibernate框架基础——映射主键属性
    (转)Hibernate框架基础——映射普通属性
    (转)Eclipse在线配置Hibernate Tools
    人物志---川航8633事件
    日常英语---200204(moderately)
  • 原文地址:https://www.cnblogs.com/Yjianyong/p/2375042.html
Copyright © 2020-2023  润新知