Java Object 序列化成XML以及XML反序列化成Java Object

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
          package org.sl.bean;

          import java.beans.XMLDecoder;
          import java.beans.XMLEncoder;
          import java.io.BufferedInputStream;
          import java.io.BufferedOutputStream;
          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.io.Serializable;

          public class ObjectXmlSerial {
             
              public static void main(String[] args) throws IOException{
                  UserBean user = new UserBean();
                  OtherUserInfoBean otherUserInfo = new OtherUserInfoBean();
                 
                  otherUserInfo.setAddress("漢字");
                  otherUserInfo.setEmail("test@test.com");
                 
                  user.setName("hello");
                  user.setPassword("world");
                 
                  user.setOtherUserInfo(otherUserInfo);
                                   
                  ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                  BufferedOutputStream bufferOut = new BufferedOutputStream(byteArrayOut);
                 
                  writeObjectToXML(bufferOut, user);
                  byte[] bys = byteArrayOut.toByteArray();
                 
                  byteArrayOut.close();
                  bufferOut.close();
                 
                 
                  ByteArrayInputStream byteArrayIn = new ByteArrayInputStream(bys);
                  BufferedInputStream bufferIn = new BufferedInputStream(byteArrayIn);
                 
                  UserBean user1 = readObjectFromXML(bufferIn);
                 
                  byteArrayIn.close();
                  bufferIn.close();       
                 
                  System.out.println(user1.getName());
                  System.out.println(user1.getOtherUserInfo().getAddress());
              }
             
              public static <T extends Serializable> void writeObjectToXML(OutputStream out, T obj){
                  XMLEncoder xmlEncoder = null;
                 
                  try{
                      xmlEncoder = new XMLEncoder(out);
                      xmlEncoder.writeObject(obj);
                  }finally{
                      if(null != xmlEncoder)
                          xmlEncoder.close();
                  }
              }
             
              @SuppressWarnings("unchecked")
              public static <T extends Serializable> T readObjectFromXML(InputStream in){
                  T obj = null;
                  XMLDecoder xmlDecoder = null;
                 
                  try{
                      xmlDecoder = new XMLDecoder(in);
                      obj = (T) xmlDecoder.readObject();
                  }finally{
                      if(null != xmlDecoder)
                          xmlDecoder.close();
                  }
                  return obj;
              }
          }


          Kyle Wang

          posted on 2011-07-04 20:55 王樹東 閱讀(1713) 評論(0)  編輯  收藏 所屬分類: Java Skills Learning and SharingCode Templates

          <2011年7月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計

          公告

          常用鏈接

          留言簿

          隨筆分類(17)

          隨筆檔案(15)

          文章分類(4)

          文章檔案(5)

          收藏夾(4)

          Algorithm

          Design

          Environment Setup

          Installer

          Maven

          MINA

          OS

          Skills for Java

          VIM

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大余县| 封开县| 光泽县| 南漳县| 周宁县| 宜宾市| 姚安县| 青冈县| 桦南县| 天祝| 白山市| 桂阳县| 从化市| 织金县| 亚东县| 德阳市| 麻江县| 万载县| 扶余县| 紫云| 麻栗坡县| 鲁山县| 连南| 鄢陵县| 昌吉市| 建始县| 江城| 新郑市| 万源市| 静乐县| 永春县| 林周县| 临城县| 定远县| 八宿县| 合山市| 靖西县| 田林县| 邵阳市| 临清市| 宁海县|