在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和開發文檔了。有空我寫個教程上來。
官方主頁:http://simple.sourceforge.Net/home.PHP
建議在看之前,花10分鐘看看開發指南,基本上就看了八九不離十了:http://simple.sourceforge.Net/download/stream/doc/tutorial/tutorial.PHP
下面給兩個例子,第一個簡單的,來自開發指南:
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
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 message
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
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
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());
}
}
生成的文件:
450000
控制台輸出的內容:
張三
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的時候會拋出一個莫名其妙的異常。