BlogJava 聯系 聚合 管理  

          Blog Stats

          隨筆檔案

          exams


          java世界

          世界Java

          XMLENCODER是一個可以將一個JAVA對象以XML方式存儲,在需要再次使用該對象時候可以使用XMLDECODER進行還原,比JAVA對象流好用.


          測試程序:


          package com.sitechasia.test.beans;


          import java.io.OutputStream;


          import junit.framework.TestCase;


          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;


          import com.funambol.framework.engine.source.ContentType;
          import com.funambol.framework.engine.source.SyncSourceInfo;
          import com.sitechasia.test.tools.BeanXmlEncoder;
          import com.sitechasia.uab.sync.source.UabSyncSource;


          public class TestBeanXmlEncoder extends TestCase {


           public TestBeanXmlEncoder(String arg0) {
            super(arg0);
           }


           protected void setUp() throws Exception {
            super.setUp();
           }


           Log log = LogFactory.getLog(TestBeanXmlEncoder.class);


           public void testEncodeUabSyncSource() {
            OutputStream beanout = System.out;
            UabSyncSource us = null;
            try {
             us = new UabSyncSource();
             us.setDebug(true);
             us.setName("card3");
             us.setSourceURI("card3");


             SyncSourceInfo info = null;
             ContentType[] cts = new ContentType[3];
             cts[0] = new ContentType("text/vcard", "2.1");
             cts[1] = new ContentType("text/x-vcard", "2.1");
             cts[2] = new ContentType("text/plain", "2.1");
             info = new SyncSourceInfo(cts, 1);
             us.setInfo(info);


             BeanXmlEncoder.EncoderBean(us, beanout);


            } catch (Exception e) {
             System.out.println("Fail to sav Bean");
            }
           }


           


           protected void tearDown() throws Exception {
            super.tearDown();
           }


          }


          工具類 :


          package com.sitechasia.test.tools;


          import java.beans.XMLEncoder;
          import java.io.OutputStream;


          import org.apache.commons.lang.builder.ToStringBuilder;
          import org.apache.commons.lang.exception.ExceptionUtils;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;


          /**
           * 對一個JAVA類進行編碼保存
           * @author  MCSA 
           *
           */
          public class BeanXmlEncoder {
           final static Log log = LogFactory.getLog(BeanXmlEncoder.class);
           public static void EncoderBean(Object bean, OutputStream beanstore) {
            XMLEncoder encoder = null;
            try {
             encoder = new XMLEncoder(beanstore);
             encoder.writeObject(bean);
             encoder.flush();
             encoder.close();
            } catch (Exception e) {
             log.error("Fail Encode bean"+ToStringBuilder.reflectionToString(bean));
             log.error("---------------------------------------------------------
          ");
             log.error(ExceptionUtils.getStackTrace(e));
            }


           }


          }

          posted on 2007-10-15 10:44 java2java 閱讀(3594) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 宜阳县| 铁岭市| 乌拉特前旗| 昌都县| 台中县| 舒兰市| 潢川县| 甘谷县| 区。| 宁阳县| 武穴市| 东方市| 大悟县| 马鞍山市| 蕲春县| 永吉县| 五大连池市| 怀化市| 福州市| 佛学| 高台县| 耒阳市| 辉南县| 德保县| 古交市| 宁强县| 河源市| 和田市| 栾川县| 夏河县| 浦东新区| 大竹县| 景宁| 长春市| 武陟县| 阳春市| 嵩明县| 水富县| 和硕县| 嘉黎县| 定兴县|