Simple---強大的XOM工具
在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());
}
}
輸入的文件內容:
<
example
index
="123"
>
<
text
>Example message
</
text
>
<
flag
>false
</
flag
>
<
slist
class
="java.util.ArrayList"
/>
</
example
>
控制太輸出信息:
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<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());
}
}
生成的文件:
<
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
>
控制檯輸出的內容:
張三
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的開發團隊,幫我殺出重圍!