在Java应用中做XML与Java对象的双向转换往往是件费力不讨好的事情,如果一个应用的核心数据交换是以XML为核心的,做这种转换往往风险很大,真正复杂的地方其实是XML到Java的转换。
本人也算一个Java的*分子,曾经对SUN提供的类库深信不疑,对Apache提供的组件礼顶膜拜,结果狠狠被SUN和Apache恶心了一把-----SUN的XML API像肿瘤一样畸形发展,尤其到了J2EE规范部分,复杂的令人望而却步。Apache提供了几个XML组建工具,与Java互转的有Betwixt2,结果bug多多,像漏气的车胎,你无法依赖它能走多远!
郁闷之极,已经到了山穷水尽的地步,眼下项目就尾声了,总不能退回到石器时代重拾DOM4j吧!到open-open上找找,终于柳暗花明,Simple---强大的XOM工具,横扫我心中的疑云,Simple!
经过一些测试,Simple果然不负众望,以前遇到的问题轻松搞定!
一个多小时时间就可以熟悉了里面的API和开发文档了。有空我写个教程上来。
官方主页:
建议在看之前,花10分钟看看开发指南,基本上就看了八九不离十了:
下面给两个例子,第一个简单的,来自开发指南:
package test1;
import org.simpleframework.xml.*;
import org.simpleframework.xml.core.Persister;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
@Element()
private boolean flag;
@Element(required = false)
private Integer num;
@ElementList(required = false)
private List<String> slist = new ArrayList<String>();
public Example() {
super();
}
public Example(String text, int index) {
this.text = text;
this.index = index;
// slist.add("sdf");
}
public String getMessage() {
return text;
}
public int getId() {
return index;
}
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("outxml/example.xml");
serializer.write(example, result);
Example _obj = serializer.read(Example.class, result);
System.out.println(_obj.getMessage());
}
}
import org.simpleframework.xml.*;
import org.simpleframework.xml.core.Persister;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
@Element()
private boolean flag;
@Element(required = false)
private Integer num;
@ElementList(required = false)
private List<String> slist = new ArrayList<String>();
public Example() {
super();
}
public Example(String text, int index) {
this.text = text;
this.index = index;
// slist.add("sdf");
}
public String getMessage() {
return text;
}
public int getId() {
return index;
}
public static void main(String[] args) throws Exception {
Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("outxml/example.xml");
serializer.write(example, result);
Example _obj = serializer.read(Example.class, result);
System.out.println(_obj.getMessage());
}
}
输入的文件内容:
<example index="123">
<text>Example message</text>
<flag>false</flag>
<slist class="java.util.ArrayList"/>
</example>
<text>Example message</text>
<flag>false</flag>
<slist class="java.util.ArrayList"/>
</example>
控制太输出信息:
Example message
Process finished with exit code 0
Process finished with exit code 0
第二个,我自己定制的,仅仅省略了getter/setter:
@Root
public class Customer {
@Attribute(required = true)
private Long id;
@Element(required = true)
private String name;
@Element(required = false)
private Address address;
@ElementList(required = false, type = Order.class)
private List<Order> orderList = new ArrayList<Order>(0);
public Customer() {
}
public Customer(Long id, String name) {
this.id = id;
this.name = name;
}
@Root
public class Address {
@Element(required = true)
private String code;
@Element
private String street;
@Element
private boolean isopen;
public Address() {
}
public Address(String code, String street, boolean isopen) {
this.code = code;
this.street = street;
this.isopen = isopen;
}
@Root
public class Order {
@Attribute(required = true)
private Long id;
@Element(required = true)
private Date cdate;
@ElementList(required = false, type = Item.class)
private List<Item> itemList = new ArrayList<Item>(0);
public Order() {
}
public Order(Long id, Date cdate) {
this.id = id;
this.cdate = cdate;
}
@Root
public class Item {
@Attribute
private Long id;
@Element(required = true)
private int num;
@Element(required = false)
private BigDecimal price;
public Item() {
}
public Item(int num, Long id) {
this.num = num;
this.id = id;
}
public class Customer {
@Attribute(required = true)
private Long id;
@Element(required = true)
private String name;
@Element(required = false)
private Address address;
@ElementList(required = false, type = Order.class)
private List<Order> orderList = new ArrayList<Order>(0);
public Customer() {
}
public Customer(Long id, String name) {
this.id = id;
this.name = name;
}
@Root
public class Address {
@Element(required = true)
private String code;
@Element
private String street;
@Element
private boolean isopen;
public Address() {
}
public Address(String code, String street, boolean isopen) {
this.code = code;
this.street = street;
this.isopen = isopen;
}
@Root
public class Order {
@Attribute(required = true)
private Long id;
@Element(required = true)
private Date cdate;
@ElementList(required = false, type = Item.class)
private List<Item> itemList = new ArrayList<Item>(0);
public Order() {
}
public Order(Long id, Date cdate) {
this.id = id;
this.cdate = cdate;
}
@Root
public class Item {
@Attribute
private Long id;
@Element(required = true)
private int num;
@Element(required = false)
private BigDecimal price;
public Item() {
}
public Item(int num, Long id) {
this.num = num;
this.id = id;
}
package test;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.util.Date;
import java.io.File;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2008-12-29 22:05:16
*/
public class TestSimple {
public static void main(String[] args) throws Exception {
Customer customer = new Customer(111L, "张三");
Order order1 = new Order(1L, new Date());
Order order2 = new Order(2L, new Date());
Item item11 = new Item(1, 11L);
Item item21 = new Item(2, 21L);
Address address = new Address("450000", "瑞达路XX#", true);
customer.setAddress(address);
customer.getOrderList().add(order1);
customer.getOrderList().add(order2);
order1.getItemList().add(item11);
order2.getItemList().add(item21);
Serializer serializer = new Persister();
File result = new File("outxml/customer.xml");
serializer.write(customer, result);
Customer _obj = serializer.read(Customer.class, result);
System.out.println(_obj.getName());
System.out.println(_obj.getOrderList().get(0).getCdate());
}
}
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import java.util.Date;
import java.io.File;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 2008-12-29 22:05:16
*/
public class TestSimple {
public static void main(String[] args) throws Exception {
Customer customer = new Customer(111L, "张三");
Order order1 = new Order(1L, new Date());
Order order2 = new Order(2L, new Date());
Item item11 = new Item(1, 11L);
Item item21 = new Item(2, 21L);
Address address = new Address("450000", "瑞达路XX#", true);
customer.setAddress(address);
customer.getOrderList().add(order1);
customer.getOrderList().add(order2);
order1.getItemList().add(item11);
order2.getItemList().add(item21);
Serializer serializer = new Persister();
File result = new File("outxml/customer.xml");
serializer.write(customer, result);
Customer _obj = serializer.read(Customer.class, result);
System.out.println(_obj.getName());
System.out.println(_obj.getOrderList().get(0).getCdate());
}
}
生成的文件:
<customer id="111">
<name>张三</name>
<address>
<code>450000</code>
<street>瑞达路XX#</street>
<isopen>true</isopen>
</address>
<orderList class="java.util.ArrayList">
<order id="1">
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList class="java.util.ArrayList">
<item id="11">
<num>1</num>
</item>
</itemList>
</order>
<order id="2">
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList class="java.util.ArrayList">
<item id="21">
<num>2</num>
</item>
</itemList>
</order>
</orderList>
</customer>
<name>张三</name>
<address>
<code>450000</code>
<street>瑞达路XX#</street>
<isopen>true</isopen>
</address>
<orderList class="java.util.ArrayList">
<order id="1">
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList class="java.util.ArrayList">
<item id="11">
<num>1</num>
</item>
</itemList>
</order>
<order id="2">
<cdate>2008-12-29 23:36:36.390 CST</cdate>
<itemList class="java.util.ArrayList">
<item id="21">
<num>2</num>
</item>
</itemList>
</order>
</orderList>
</customer>
控制台输出的内容:
张三
Mon Dec 29 23:36:36 CST 2008
Process finished with exit code 0
Mon Dec 29 23:36:36 CST 2008
Process finished with exit code 0
总结下:
Simple的确简单,好用,是一个优良的XML Object Mapping转换工具,简单到没有必要对API进行苦心积虑的研究和摸索。
Simple可以灵活定制XML的格式、元素名、命名空间、集合、数组、复杂对象等等支持都很好,并且Simple还可以验证XML是否可以正确转换为对象。
Simple要求每个Bean必须提供一个默认的构造方法,这是个开发参考中没有明确提出的要求,我经过大量测试发现的。否则,你再从XML转Java的时候会抛出一个莫名其妙的异常。
感谢Simple的开发团队,帮我杀出重围!
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/123382,如需转载请自行联系原作者