原创作者: 尹标平   阅读:1250次   评论:0条   更新时间:2011-06-06    
本文介绍bbossgroups 中的对象xml序列化技术,涵盖基础数据类型、复杂对象、异常对象、文件对象、二进制数组、容器对象(List,Map,Set,Array)以及各种类型的组合结构,其特点是api简单,转换效率高,生成的xml简洁易懂,可读性好,可以通过aop框架组件管理容器直接加载xml串获取其中的对象。切入正题。


对象xml序列化组件及接口 Top

1.对象xml序列化组件
org.frameworkset.soa.ObjectSerializable
2.对象xml序列化接口
ArrayBean bean1 = new ArrayBean();
String xmlcontent = ObjectSerializable.convertBeanObjectToXML("beanname",beanObject ,ArrayBean.class);
3.xml反序列化接口
ArrayBean bean1 = ObjectSerializable.convertXMLToBeanObject("beanname",xmlcontent,ArrayBean.class);

对象xml序列化技术实例 Top

对象xml序列化技术非常简单,本实例展示了以下功能:
a.如何将包含文件和异常的对象序列化成xml串,然后又从xml串中恢复这些对象
b.如何将List对像及内部的对象序列化成xml串,然后又从xml串中恢复这些对象

需要特别说明一点的是,文件对象的序列化,我们直接将文件对象对应的文件内容以二进制流的方式序列化到xml串中,反序列化时将文件的二进制流保存在一个临时文件对象中,用户可以很方便地访问文件的内容(后续可以可虑通过注解指定要存放的临时文件的目录,现在直接采用了os默认的临时目录)。

1.对象xml序列化技术实例类
/*
 *  Copyright 2008-2010 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.frameworkset.soa;



import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.frameworkset.util.ValueObjectUtil;


/**
 * <p>Title: SOAApplicationContext.java</p> 
 * <p>Description: </p>
 * <p>bboss workgroup</p>
 * <p>Copyright (c) 2008</p>
 * @Date 2011-5-9 下午06:12:52
 * @author biaoping.yin
 * @version 1.0
 */
public class SOAApplicationContextTest {
	@Test
	public void bytearraybeantoxml() throws NumberFormatException, IllegalArgumentException, IntrospectionException
	{
		ArrayBean bean = new ArrayBean();
		Exception e = new Exception("异常发生。");
		bean.setE(e);
		
		String content = "<?xml version=\"1.0\" encoding=\"gbk\"?>" +
		"<esb>"+
			"<call>"+
			
			"<!-- 调度中心需要的数据开始 -->"+
				
				"<property name=\"soamethodcall\" " +
					"class=\"org.frameworkset.soa.SOAMethodCall\" "+
					"f:requestor=\"requestor\" "+
					"f:requestid=\"1000000\" "+
					"f:password=\"requestpassword\" "+
					"f:encypt=\"encrypt\" "+
					"f:encyptalgorithem=\"algorithm\" "+
					"f:serviceid=\"hilaryserviceid\" "+
					"f:issynchronized=\"true\" >"+
					"<!-- 调度中心需要的数据结束 -->"+
					"<!-- 调度中心提交给服务提供方的服务方法信息 -->"+
					"<property name=\"soamethodinfo\" class=\"org.frameworkset.soa.SOAMethodInfo\" " +
													"f:methodName=\"methodname\">"+
						"<property name=\"paramTypes\">"+
							"<array componentType=\"Class\">"+
								"<property >String</property>"+
								"<property >String</property>"+
								"<property >String[]</property>"+
							"</array>"+
						"</property>" +
						"<property name=\"params\">"+
							"<array componentType=\"object\">"+
								"<property name=\"condition\">1=1</property>"+
								"<property name=\"orderby\">order by ${A}</property>"+
								"<property>"+
								"	<array componentType=\"String\">"+
									"<property>A</property>"+
									"<property>B</property>"+
									"</array>"+
								"</property>"+
							"</array>"+
						"</property>" +
					"</property>" +
				"</property>"+
			"</call>"+
		"</esb>";
		bean.setArrays(content.getBytes() );
		String xmlcontent = ObjectSerializable.convertBeanObjectToXML("beanarray",bean, bean.getClass());
		System.out.println(xmlcontent);
		ArrayBean bean1 = ObjectSerializable.convertXMLToBeanObject("beanarray",xmlcontent,ArrayBean.class);
		System.out.println(new String(bean1.getArrays()));
		System.out.println(bean1.getE());
		
	}
	
	@Test
	public void filebeantoxml() throws Exception
	{
		FileBean bean = new FileBean();
		
		bean.setFile(ValueObjectUtil.getClassPathFile("org/frameworkset/soa/datasource-sql.xml"));
		String xmlcontent = ObjectSerializable.convertBeanObjectToXML("beanfile",bean, bean.getClass());
		System.out.println(xmlcontent);
		FileBean bean1 = ObjectSerializable.convertXMLToBeanObject("beanfile",xmlcontent,FileBean.class);
		System.out.println(ValueObjectUtil.getFileContent(bean1.getFile(),"UTF-8"));
	}
	@Test
	public void beanstoxml() throws Exception
	{
		FileBean fbean = new FileBean();
		
		fbean.setFile(ValueObjectUtil.getClassPathFile("org/frameworkset/soa/datasource-sql.xml"));
		ArrayBean bean = new ArrayBean();
		String content = "<?xml version=\"1.0\" encoding=\"gbk\"?>" +
		"<esb>"+
			"<call>"+
			
			"<!-- 调度中心需要的数据开始 -->"+
				
				"<property name=\"soamethodcall\" " +
					"class=\"org.frameworkset.soa.SOAMethodCall\" "+
					"f:requestor=\"requestor\" "+
					"f:requestid=\"1000000\" "+
					"f:password=\"requestpassword\" "+
					"f:encypt=\"encrypt\" "+
					"f:encyptalgorithem=\"algorithm\" "+
					"f:serviceid=\"hilaryserviceid\" "+
					"f:issynchronized=\"true\" >"+
					"<!-- 调度中心需要的数据结束 -->"+
					"<!-- 调度中心提交给服务提供方的服务方法信息 -->"+
					"<property name=\"soamethodinfo\" class=\"org.frameworkset.soa.SOAMethodInfo\" " +
													"f:methodName=\"methodname\">"+
						"<property name=\"paramTypes\">"+
							"<array componentType=\"Class\">"+
								"<property >String</property>"+
								"<property >String</property>"+
								"<property >String[]</property>"+
							"</array>"+
						"</property>" +
						"<property name=\"params\">"+
							"<array componentType=\"object\">"+
								"<property name=\"condition\">1=1</property>"+
								"<property name=\"orderby\">order by ${A}</property>"+
								"<property>"+
								"	<array componentType=\"String\">"+
									"<property>A</property>"+
									"<property>B</property>"+
									"</array>"+
								"</property>"+
							"</array>"+
						"</property>" +
					"</property>" +
				"</property>"+
			"</call>"+
		"</esb>";
		bean.setArrays(content.getBytes() );
		List beans = new ArrayList();
		beans.add(fbean);
		beans.add(bean);
		
		String xmlcontent = ObjectSerializable.convertBeanObjectToXML("listObject",beans, List.class);
		System.out.println(xmlcontent);
		
		List copybeans = ObjectSerializable.convertXMLToBeanObject("listObject", xmlcontent, List.class);
		System.out.println(copybeans.size());
	}
}


2.相关的对象类-ArrayBean
/*
 *  Copyright 2008-2010 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.frameworkset.soa;

/**
 * <p>Title: ArrayBean.java</p> 
 * <p>Description: </p>
 * <p>bboss workgroup</p>
 * <p>Copyright (c) 2008</p>
 * @Date 2011-5-14 上午11:31:32
 * @author biaoping.yin
 * @version 1.0
 */
public class ArrayBean {
	private byte[] arrays;
	private Exception e;

	/**
	 * @return the arrays
	 */
	public byte[] getArrays() {
		return arrays;
	}

	/**
	 * @param arrays the arrays to set
	 */
	public void setArrays(byte[] arrays) {
		this.arrays = arrays;
	}

	/**
	 * @return the e
	 */
	public Exception getE() {
		return e;
	}

	/**
	 * @param e the e to set
	 */
	public void setE(Exception e) {
		this.e = e;
	}
	

}


3.相关的对象类-FileBean
/*
 *  Copyright 2008-2010 biaoping.yin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.frameworkset.soa;

import java.io.File;

/**
 * <p>Title: FileBean.java</p> 
 * <p>Description: </p>
 * <p>bboss workgroup</p>
 * <p>Copyright (c) 2008</p>
 * @Date 2011-5-14 上午11:32:24
 * @author biaoping.yin
 * @version 1.0
 */
public class FileBean {
	private File file;
  
	/**
	 * @return the file
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @param file the file to set
	 */
	public void setFile(File file) {
		this.file = file;
	}
}

对象xml序列化技术实战策略 Top

对象xml序列化技术实战策略
1.从附件中下载本文涉及的实例eclipse工程xmlserializable.zip
http://dl.iteye.com/topics/download/d534eae3-c4c2-3afd-8405-30befd4acfd8
2.将xmlserializable.zip 解压后将工程导入eclipse开发环境
3.执行测试用例,即可查看本文中涉及功能的实际效果
/xmlserializable/test/org/frameworkset/soa/SOAApplicationContextTest.java
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics