目前最新版本为 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);
1万+




被折叠的 条评论
为什么被折叠?



