用Apache Commons Betwixt2 在XML与Java对象之间相互转换
如果你需要做XML与Java对象之间相互转换,apache betwixt2是一个不二的选择。
官方主页:
[url]http://commons.apache.org/betwixt/[/url] 目前最新版本是0.8.
以前我用XStream,但是对比起来还是没apache betwixt2来得容易。下面给些例子出来。
/**
* 测试Bean
*/
public class PersonBean {
private String name;
private int age;
public PersonBean() {
}
public PersonBean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
public String toString() {
return "PersonBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
* 测试Bean
*/
public class PersonBean {
private String name;
private int age;
public PersonBean() {
}
public PersonBean(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge( int age) {
this.age = age;
}
public String toString() {
return "PersonBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.io.StringReader;
import org.apache.commons.betwixt.io.BeanReader;
/**
* XML转Java
*/
public class ReadExampleApp {
public static final void main(String args[]) throws Exception {
//创建一个读取xml文件的流
StringReader xmlReader = new StringReader(
"<?xml version='1.0' ?><person><age>25</age><name>James Smith</name></person>");
//创建一个BeanReader实例,相当于转换器
BeanReader beanReader = new BeanReader();
//配置BeanReader实例
beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives( false);
beanReader.getBindingConfiguration().setMapIDs( false); //不自动生成ID
//注册要转换对象的类,并指定根节点名称
beanReader.registerBeanClass( "person", PersonBean. class);
//将XML解析Java Object
PersonBean person = (PersonBean) beanReader.parse(xmlReader);
//在控制台输出对象
System.out.println(person);
}
}
import org.apache.commons.betwixt.io.BeanReader;
/**
* XML转Java
*/
public class ReadExampleApp {
public static final void main(String args[]) throws Exception {
//创建一个读取xml文件的流
StringReader xmlReader = new StringReader(
"<?xml version='1.0' ?><person><age>25</age><name>James Smith</name></person>");
//创建一个BeanReader实例,相当于转换器
BeanReader beanReader = new BeanReader();
//配置BeanReader实例
beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives( false);
beanReader.getBindingConfiguration().setMapIDs( false); //不自动生成ID
//注册要转换对象的类,并指定根节点名称
beanReader.registerBeanClass( "person", PersonBean. class);
//将XML解析Java Object
PersonBean person = (PersonBean) beanReader.parse(xmlReader);
//在控制台输出对象
System.out.println(person);
}
}
运行结果:
PersonBean{name='James Smith', age=25}
Process finished with exit code 0
Process finished with exit code 0
import betwixt.Person;
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;
import java.beans.IntrospectionException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
/**
* 将java对象转换为xml,并输出到文件
*/
public class WriteExampleApp {
public static final void main(String [] args) throws Exception {
//创建一个输出流,将用来输出Java转换的XML文件
FileWriter outputWriter = new FileWriter( "c:\\tttttttttt.txt");
//输出XML的文件头
outputWriter.write( "<?xml version='1.0' ?>");
//创建一个BeanWriter实例,并将BeanWriter的输出重定向到指定的输出流
BeanWriter beanWriter = new BeanWriter(outputWriter);
//配置BeanWriter对象
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives( false);
beanWriter.getBindingConfiguration().setMapIDs( false);
beanWriter.enablePrettyPrint();
Person person = new Person( "阿牛", 21);
beanWriter.write(person);
//关闭输出流
outputWriter.close();
}
}
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;
import java.beans.IntrospectionException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
/**
* 将java对象转换为xml,并输出到文件
*/
public class WriteExampleApp {
public static final void main(String [] args) throws Exception {
//创建一个输出流,将用来输出Java转换的XML文件
FileWriter outputWriter = new FileWriter( "c:\\tttttttttt.txt");
//输出XML的文件头
outputWriter.write( "<?xml version='1.0' ?>");
//创建一个BeanWriter实例,并将BeanWriter的输出重定向到指定的输出流
BeanWriter beanWriter = new BeanWriter(outputWriter);
//配置BeanWriter对象
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives( false);
beanWriter.getBindingConfiguration().setMapIDs( false);
beanWriter.enablePrettyPrint();
Person person = new Person( "阿牛", 21);
beanWriter.write(person);
//关闭输出流
outputWriter.close();
}
}
运行结果:
Process finished with exit code 0
(在C盘目录下创建一个名叫tttttttttt.txt的文件。
import betwixt.Person;
import org.apache.commons.betwixt.io.BeanWriter;
import java.io.StringWriter;
/**
* 将Java对象转换为XML,并获取其字符串
*
* @author leizhimin 2008-9-18 14:36:49
*/
public class WriteStringExampleApp {
public static final void main(String [] args) throws Exception {
String reslutXml;
//创建一个输出流,将用来输出Java转换的XML文件
StringWriter outputWriter = new StringWriter();
//输出XML的文件头
outputWriter.write( "<?xml version='1.0' ?>");
//创建一个BeanWriter实例,并将BeanWriter的输出重定向到指定的输出流
BeanWriter beanWriter = new BeanWriter(outputWriter);
//配置BeanWriter对象
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives( false);
beanWriter.getBindingConfiguration().setMapIDs( false);
beanWriter.enablePrettyPrint();
Person person = new Person( "阿牛", 21);
beanWriter.write(person);
reslutXml = outputWriter.toString();
//关闭输出流
outputWriter.close();
//控制台输出转换结果的字符串
System.out.println(reslutXml);
}
}
import org.apache.commons.betwixt.io.BeanWriter;
import java.io.StringWriter;
/**
* 将Java对象转换为XML,并获取其字符串
*
* @author leizhimin 2008-9-18 14:36:49
*/
public class WriteStringExampleApp {
public static final void main(String [] args) throws Exception {
String reslutXml;
//创建一个输出流,将用来输出Java转换的XML文件
StringWriter outputWriter = new StringWriter();
//输出XML的文件头
outputWriter.write( "<?xml version='1.0' ?>");
//创建一个BeanWriter实例,并将BeanWriter的输出重定向到指定的输出流
BeanWriter beanWriter = new BeanWriter(outputWriter);
//配置BeanWriter对象
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives( false);
beanWriter.getBindingConfiguration().setMapIDs( false);
beanWriter.enablePrettyPrint();
Person person = new Person( "阿牛", 21);
beanWriter.write(person);
reslutXml = outputWriter.toString();
//关闭输出流
outputWriter.close();
//控制台输出转换结果的字符串
System.out.println(reslutXml);
}
}
运行结果:
<?xml version='1.0' ?> <Person>
<addList/>
<age>21</age>
<name>阿牛</name>
</Person>
Process finished with exit code 0
<addList/>
<age>21</age>
<name>阿牛</name>
</Person>
Process finished with exit code 0
总结:
betwixt功能强大,使用简单,是快速开发不二的选择。
BeanWriter相当于一个双向转换器工厂,但是这个工厂需要装配,并且它是非线程安全的。
BeanWriter有很多配置项,可以将对象的成员装配成xml元素的属性。也可以改写元素属性的名字。默认情况下元素的名字与java成员的名字相同,根节点与java类名相同(不带包名)。并且这些操作是双向可逆的。太牛了吧!
BeanWriter还可以转换格式不规范的xml文档,比如节点无法有些不匹配,或者无法完全映射到对象上的xml,这些都不会出错,并且当节点为null的时候,转换也不用做特殊处理,非常简单。
BeanWriter在将Java转换为xml时,Java对象可以是复杂对象,对象之中嵌套对象,如果复杂对象的某个成员比如类型为另外一个对象为null时,还可以选择是否在生成xml的时候输出该节点。如果期望输出,则输出一个对应复杂对象成员的子元素,里面的元素值全为空。
这些特性是XStream所无法相比的。有关速度效率的问题,还没测试过。不过一般应用没有发现问题,除非你的xml几百M。
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/100188,如需转载请自行联系原作者