java xmldecoder_一个使用JAVA xmlencoder 例子

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));

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值