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

          導航

          統計

          公告

          常用鏈接

          留言簿

          隨筆分類(17)

          隨筆檔案(15)

          文章分類(4)

          文章檔案(5)

          收藏夾(4)

          Algorithm

          Design

          Environment Setup

          Installer

          Maven

          MINA

          OS

          Skills for Java

          VIM

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 朝阳区| 巴中市| 全州县| 郁南县| 云安县| 水富县| 宁河县| 安远县| 卫辉市| 惠安县| 黄山市| 大方县| 阿克苏市| 金山区| 永吉县| 闽清县| 青冈县| 阿拉善右旗| 涿州市| 韩城市| 邹平县| 岱山县| 隆子县| 三明市| 河北省| 宁远县| 南投县| 华容县| 安康市| 门头沟区| 大悟县| 弥勒县| 澳门| 晋城| 峨山| 铁岭市| 绩溪县| 鱼台县| 中超| 镶黄旗| 永济市|