论坛首页 Java企业应用论坛

浅谈 bbossgroups 对象xml序列化技术

浏览 1937 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (13) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-06-06   最后修改:2011-06-27
本文介绍bbossgroups 中的对象xml序列化技术,涵盖基础数据类型、复杂对象、异常对象、文件对象、二进制数组、容器对象(List,Map,Set,Array)以及各种类型的组合结构,其特点是api简单,转换效率高,生成的xml简洁易懂,可读性好,可以通过aop框架组件管理容器直接加载xml串获取其中的对象。切入正题。


目 录 [ - ]
    1.对象xml序列化技术实战策略
    2.对象xml序列化组件及接口
    3.对象xml序列化技术实例
   

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

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

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

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



   发表时间:2011-06-27  
3.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());  
        }  
    }  




3.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.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;  
        }  
    }  
0 请登录后投票
   发表时间:2011-06-29  
ainidehsj 写道
学习一下。

有什么问题,多交流,有助于bbossgroups的完善。
0 请登录后投票
   发表时间:2011-07-09  
bbossgroups项目新域名开通:
http://www.bbossgroups.com
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics