一、简单的例子
代码清单1:
一个普通的JavaBean
01 | import org.simpleframework.xml.Attribute; |
02 | import org.simpleframework.xml.Element; |
03 | import org.simpleframework.xml.Root; |
05 | public class Student { |
09 | private String stu_no; |
11 | public String stu_type; |
13 | private String stu_name; |
15 | private String stu_class; |
17 | private String stu_year; |
20 | public String getStu_type() { |
23 | public void setStu_type(String stu_type) { |
24 | this .stu_type = stu_type; |
26 | public String getStu_class() { |
29 | public void setStu_class(String stu_class) { |
30 | this .stu_class = stu_class; |
32 | public String getStu_year() { |
35 | public void setStu_year(String stu_year) { |
36 | this .stu_year = stu_year; |
38 | public String getStu_no() { |
41 | public void setStu_no(String stu_no) { |
44 | public int getStu_id() { |
47 | public void setStu_id( int stu_id) { |
50 | public String getStu_name() { |
53 | public void setStu_name(String stu_name) { |
54 | this .stu_name = stu_name; |
代码清单2:
01 | public class SimpleXMLTest { |
06 | public void testCreateXml() { |
09 | Serializer serializer= new Persister(); |
10 | Student stu = new Student(); |
12 | stu.setStu_no( "201205001" ); |
13 | stu.setStu_type( "经济学" ); |
14 | stu.setStu_name( "慕容雪" ); |
15 | stu.setStu_class( "005班" ); |
16 | stu.setStu_year( "2012" ); |
17 | File result = new File( "./src/student.xml" ); |
18 | serializer.write(stu, result); |
19 | } catch (Exception e) { |
执行后即可创建一个student.xml文档,内容如下:
2 | < stu_no >201205001</ stu_no > |
3 | < stu_type >经济学</ stu_type > |
4 | < stu_name >慕容雪</ stu_name > |
5 | < stu_class >005班</ stu_class > |
6 | < stu_year >2012</ stu_year > |
是不是很简单?和那些Dom4j,Parser相比,更容易操作。
读取XML也变得异常简单
02 | public void testReadXML(){ |
06 | Serializer serializer = new Persister(); |
07 | File source = new File( "./src/student.xml" ); |
08 | Student stu=serializer.read(Student. class , source); |
09 | System.out.println(stu.getStu_name()); |
10 | System.out.println(stu.getStu_no()); |
11 | System.out.println(stu.getStu_type()); |
12 | System.out.println(stu.getStu_year()); |
13 | System.out.println(stu.getStu_class()); |
14 | System.out.println(stu.getStu_id()); |
16 | } catch (Exception e) { |
相比那些要获取节点,获得元素,操作各种Dom模型的传统方式,是不是清晰明了了许多。
目前我正在按照官方文档的Demo 测试对各种数据对象的支持.例子在最后共享在115网盘。
Demo文档还是比较丰富的,建议大家通读一遍。
二、总体感觉:
1.上手很快
2.操作简便易懂
3.基于注解实现,零配置(现在貌似零配置很流行)
4.可以自定义模板XML(嘻嘻,和velocity不一样的)
5.支持序列化和反序列化
6.泛型支持比较好
缺点:
1.必须要依赖POJO类
2.只能生成XML
三、使用要点
在simpleXml中这个类很重要
Serializer接口 及其实现子类Persister
1 | Serializer serializer= new Persister(); |
将POJO类写入XML
1 | serializer.write(Object object, File file); |
读取XML内容
1 | Object object=serializer.read(Object. class , File file); |
几种常用注解
2 | @ElementArray (entry= "add" ) |
Q:inline参数是什么意思
A:对于如下结构的xml节点
<propertyList>
<name>example
</name>
<entryList>
<entry
key=
"one"
>
<value>first value
</value>
</entry>
<entry
key=
"two"
>
<value>second value
</value>
</entry>
<entry
key=
"three"
>
<value>third value
</value>
</entry>
</entryList>
</propertyList>
entry节点其实是一个List,不过又没有用这种写法
<propertyList>
<name>example
</name>
<entry
key=
"one"
>
<value>first value
</value>
</entry>
<entry
key=
"two"
>
<value>second value
</value>
</entry>
<entry
key=
"three"
>
<value>third value
</value>
</entry>
</propertyList>
所以在Java的类中对应的时候需要使用inline标签
@ElementList (entry =
"entry" , required =
true , inline =
true )
public List < entry > entryList ;
Q:entry参数是什么
A:当Java类中的节点名称和xml节点不相对应的时候,需要用entry指定xml文件中的节点名称
如
<root>
<username>aaa
</username>
<password>bbb
</password>
<abc
key=
"Key0"
value=
"Value0"
/>
<abc
key=
"Key1"
value=
"Value1"
/>
<abc
key=
"Key2"
value=
"Value2"
/>
</root>
Java类中对应为
@Root(name =
"root")
public
class
GlobalUserInfoConfig {
@Element(required =
true)
public String username;
@Element(required =
true)
public String password;
@ElementList(entry =
"abc", required =
true, inline =
true)
public List<GlobalParameter> globalConfigList;
}