Simple是一個XML序列化框架,一個
Java版本寬容的序列化框架,能夠快速在
Java平臺上開發XML。支持通過annotations完全配置化的XML結構;提供版本管理框架允許向前和向后兼容序列化;更好的性能,使用輕量級StAX提升XML反序列化
進程,比XStream和JAXB更快;通過namespace annotations提供完全的命名空間支持;包括XML模板系統
目前最新版本為 2.0.1. 官網地址:
http://simple.sourceforge.net/home.php
注:不過筆者認為,如果你使用JavaSE 6.0,則建議使用其自帶的
JAXB(Java Architecture for XML Binding).其使用非常簡單,支持通過annotations完全配置化的XML結構。而且其也是Java中的一個規范實現,
JAX-RS規范就使用JAXB來實現對象的序列化功能。
下面是一個簡單示例:
先定義一個Example對象, 我們只需要簡單的加上Annoation后,就可以進行對象與XML之間的轉換。
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
public Example() {
super();
}
public Example(String text, int index) {
this.text = text;
this.index = index;
}
public String getMessage() {
return text;
}
public int getId() {
return index;
}
}
進行XML序列化操作:
Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("example.xml");
serializer.write(example, result);
下面是生成的XML文件內容:
<example index="123">
<text>Example message</text>
</example>
從XML文件中,反序列化后,得到Example對象實例:
Serializer serializer = new Persister();
File source = new File("example.xml");
Example example = serializer.read(Example.class, source);
到現在基本的演示完成,API使用起來非常簡單。
查看更多示例(包括級聯對象的序列化與反序列化操作)
Good Luck!
Yours Matthew!
posted on 2008-12-10 20:16
x.matthew 閱讀(3831)
評論(1) 編輯 收藏 所屬分類:
Best Practise(JDK API)