一个自己编写的xml和JAVA对象之间的转换工具,xml和对象轻松转换(simple-xml2object)

来源:互联网 发布:淘宝俪兰化妆品怎么样 编辑:程序博客网 时间:2024/05/17 04:22

               simple-xml2object组件禁止用于商业用途,作者:左潇龙。

               前段时间公司的WebService接口需要处理xml数据,主要就是和对象之间的互相转换,传输的时候用xml,后台使用的时候转换成对象,所以就有了xml和object之间经常的互相转换。

                因为我们的项目使用的JDK是1.5,所以并没有JAXB,如果要使用的话需要加入JAXB相关的jar包,但是感觉有点过大,而且使用起来有很多限制,而这些限制对我们的项目来说,毫无意义。比如在解析XML时,必须含有相关的命名空间,这使得我不得不在解析之前加入命名空间,因为WEBSERVICE的服务端并没有帮我们加入命名空间。

                所以自己基于DOM开发了一个简易组件,可以实现xml和object的互相转换,功能简单实用,带有中文异常处理,虽说没英文显得专业,但感觉中文更实用,其中提示了所有异常产生最有可能的原因,还附带纯中文CHM文档,并保留了扩展性,有兴趣的也可以自己实现自己的转换机制。

                由于引入了注解和泛型,所以需要JDK在1.5以上,另外如果你项目的JDK是1.6甚至1.7(估计1.7的不多),那就不再需要simple-xml2object了,因为JAXB已经被带入JDK,再使用就失去了simple-xml2object的意义,所以简单点说simple-xml2object适用于项目JDK版本为1.5的

                在此分享给各位,如果有同样需求的,可以考虑,jar包大小26KB,因为全部基于JDK自带的类库,所以无需其余依赖包。

                另外如果哪位在使用过程中有发现bug或者其他问题,可以在此留言,或者发我邮箱150349407@qq.com,我会及时更改。

                目前最初始的版本1.0.0主要支持的功能有:

                1.xml转换成object,并且可扩展实现自己的解析器,另外目前还不支持直接转换成一个对象的list,但是可以重复调用解析方法,同样可以产生一个list,有时间的话,下一版本会考虑直接支持此功能。

                2.object转换成xml,支持xml格式的配置,包含无格式,只带换行的格式以及标准格式(也就是格式化以后的样子)。

                3.日期格式采用注解方式,可直接在属性上面设置日期格式。

                4.考虑到我们平时项目中对象的一个序列属性大部分是list或者是set的,所以此版本只支持属性为list或者set的,相信足够了。

                jar包和CHM文档我已经打包,在我的资源里面可以免费下载,地址:http://download.csdn.net/detail/zuoxiaolong8810/5161785。

                下面是我专门为此组件写的一个测试类,用法简单明了,有详细的注释。

import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.xml2object.simple.annotation.ADate;import com.xml2object.simple.container.ConfigurableContainer;import com.xml2object.simple.container.Container;import com.xml2object.simple.processor.XmlBulider;import com.xml2object.simple.processor.XmlBulider.Format;import com.xml2object.simple.support.DefaultConfigurableContainer;public class Test {String stringParam = "stringValue";@ADate(format = "yyyy-MM-dd hh")//日期注解,设置日期格式Date dateParam = new Date();Test testParam;List<Test> testListParam;public static void main(String[] args) throws IOException {//不可配置的容器Container container = new DefaultConfigurableContainer(Test.class);//向容器中添加一个复杂的Test对象container.add(createTest());//获取容器自动解析的xml内容String xml = container.getXml();//将xml内容存放在一个文件中write("E:/test1.xml", xml);//可配置的容器,使用可配置的容器接口,推荐此种方式,比较灵活ConfigurableContainer configurableContainer = new DefaultConfigurableContainer(Test.class);XmlBulider xmlBulider = configurableContainer.getXmlBulider();//设置构建器的xml格式xmlBulider.setFormat(Format.TAB_AND_LINE);//改变容器中的构建器configurableContainer.setXmlBulider(xmlBulider);//向可配置容器添加复杂对象configurableContainer.add(createTest());//获取容器自动解析的xml内容,比较下不能配置的容器构建的xml格式和日期格式String configXml = configurableContainer.getXml(0);//等同于getXml()//将xml内容存放在一个文件中write("E:/test2.xml", configXml);//再将xml从test2.xml中读取出来String readableConfigXml = read("E:/test2.xml");//向容器中再加入一个xmlconfigurableContainer.add(readableConfigXml);//获取容器自动解析的对象//因为之前已经加入了一个对象,所以在加入xml之前,容器中已包含一对xml和object,此时索引为1//容器维护了两个保持一致的数组,分别存放xml和object,索引规则与数组一致,从0开始Test test = configurableContainer.getObject(1);//打印容器大小System.out.println("size:" + configurableContainer.size());//打印解析的对象,打印的可能不太清楚//要想打印格式清晰,与我当初构建xml时相似,过程比较复杂,就不写那么详细了,各位可以自己加断点看对象内容System.out.println(test);}public static Test createTest(){//构造一个复杂的Test,没什么特别的,复杂就好。。。Test t = new Test();Test t1 = new Test();Test t2 = new Test();Test t3 = new Test();Test t4 = new Test();Test t5 = new Test();Test t6 = new Test();Test t7 = new Test();Test t8 = new Test();Test t9 = new Test();Test t10 = new Test();List<Test> testList2 = new ArrayList<Test>();testList2.add(t1);testList2.add(t2);List<Test> testList = new ArrayList<Test>();t10.testListParam = testList2;testList.add(t10);testList.add(t9);testList.add(t8);testList.add(t7);testList.add(t6);t5.testListParam = testList;t5.testParam = t4;t3.testParam = t5;t.testParam = t3;return t;}public String toString(){StringBuffer stringBuffer = new StringBuffer();stringBuffer.append("stringParam:" + (stringParam == null? "" : stringParam) + "\r\n").append("dateParam:" + (dateParam == null ? "" : dateParam) + "\r\n").append("testParam:" + (testParam == null ? "" : testParam) + "\r\n");if (testListParam != null) {stringBuffer.append("testListParam:\r\n");for (Test temp : testListParam) {stringBuffer.append(temp + "\r\n");}}return stringBuffer.toString();}public static String read(String fileName) throws IOException{File file = new File(fileName);FileInputStream fileInputStream = new FileInputStream(file);byte[] bytes = new byte[(int) file.length()];fileInputStream.read(bytes);return new String(bytes);}public static void write(String fileName,String xml) throws IOException{OutputStream outputStream = new FileOutputStream(new File(fileName));ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes());int len = -1;byte[] bytes = new byte[1024];while ((len = byteArrayInputStream.read(bytes)) != -1) {outputStream.write(bytes, 0, len);}outputStream.flush();outputStream.close();byteArrayInputStream.close();}}

                 新建一个java工程,将下载的jar包导入,再直接将测试类代码贴到一个java文件中,运行即可得到E盘下两个xml,可以看下我们配置的日期格式注解是否有效,另外两个xml,一个是不可配置的容器生成的,一个是我们采用可配置容器,调整格式后生成的,可以比对下其中的xml格式,是不是test2已经被格式化了呢?

                 xml格式化功能一般只是用于测试的时候使用,其主要意义是为了方便测试,因为在测试的时候,我们经常会把生成的xml打印到控制台,控制台不像编辑器可以格式化,所以这样的话看起来效果很差。当然,如果你不嫌麻烦,也可以写入一个文件,然后用编辑器打开手动格式化。

                 至于只换行的功能,一般在层级太多的时候使用,这个我想道理很简单,层次太多,打印出来的xml横向宽度会很长,也不便于观察。但是只换行的话如果层次太多其实也不太便于观察,所以从这个角度来看,这个格式貌似有些鸡肋,但我觉得完全格式化与空格式总要有个过渡,保证程序的完整性,所以就加进去了。

                 在数据传输过程中,建议使用无格式,节省传输流量。

原创粉丝点击