SimpleXml例子

来源:互联网 发布:智能语音聊天软件 编辑:程序博客网 时间:2024/06/07 22:54

一、简单的例子

代码清单1:

一个普通的JavaBean

01import org.simpleframework.xml.Attribute;
02import org.simpleframework.xml.Element;
03import org.simpleframework.xml.Root;
04@Root
05public class Student {
06    @Attribute
07    private int stu_id;
08    @Element
09    private String stu_no;
10    @Element
11    public String stu_type;
12    @Element
13    private String stu_name;
14    @Element
15    private String stu_class;
16    @Element
17    private String stu_year;
18     
19 
20    public String getStu_type() {
21        return stu_type;
22    }
23    public void setStu_type(String stu_type) {
24        this.stu_type = stu_type;
25    }
26    public String getStu_class() {
27        return stu_class;
28    }
29    public void setStu_class(String stu_class) {
30        this.stu_class = stu_class;
31    }
32    public String getStu_year() {
33        return stu_year;
34    }
35    public void setStu_year(String stu_year) {
36        this.stu_year = stu_year;
37    }
38    public String getStu_no() {
39        return stu_no;
40    }
41    public void setStu_no(String stu_no) {
42        this.stu_no = stu_no;
43    }
44    public int getStu_id() {
45        return stu_id;
46    }
47    public void setStu_id(int stu_id) {
48        this.stu_id = stu_id;
49    }
50    public String getStu_name() {
51        return stu_name;
52    }
53    public void setStu_name(String stu_name) {
54        this.stu_name = stu_name;
55    }
56 
57}

代码清单2:

01public class SimpleXMLTest {
02    /**
03     * 测试创建xml文档
04     */
05    @Test
06    public void testCreateXml() {
07         
08        try {
09            Serializer  serializer= new Persister();
10            Student stu = new Student();
11            stu.setStu_id(1);
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) {
20             
21            e.printStackTrace();
22        }
23    }
24}

执行后即可创建一个student.xml文档,内容如下:

1<student stu_id="1">
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>
7</student>

是不是很简单?和那些Dom4j,Parser相比,更容易操作。

读取XML也变得异常简单

01@Test
02    public void testReadXML(){
03         
04         
05        try {
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());
15             
16        catch (Exception e) {
17            e.printStackTrace();
18        }
19         
20    }

相比那些要获取节点,获得元素,操作各种Dom模型的传统方式,是不是清晰明了了许多。

目前我正在按照官方文档的Demo 测试对各种数据对象的支持.例子在最后共享在115网盘。

Demo文档还是比较丰富的,建议大家通读一遍。

二、总体感觉:

1.上手很快

2.操作简便易懂

3.基于注解实现,零配置(现在貌似零配置很流行)

4.可以自定义模板XML(嘻嘻,和velocity不一样的)

5.支持序列化和反序列化

6.泛型支持比较好

缺点:

1.必须要依赖POJO类 

2.只能生成XML

三、使用要点

在simpleXml中这个类很重要

 Serializer接口 及其实现子类Persister

1Serializer serializer=new Persister();

将POJO类写入XML

1serializer.write(Object object, File file);

读取XML内容

1Object object=serializer.read(Object.class, File file);

几种常用注解
1//XML的根元素,一般在类上面声明
2@Root

1//对XML属性的标识
2@Attribute

1//对XML中元素的标识   
2@Element

1//对数组对象的标识
2@ElementArray(entry="add")
view source
print?
1//对XML中集合属性的标识
2 @ElementList


Q:inline参数是什么意思

 

A:对于如下结构的xml节点

 

XML语言: Codee#14196
<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,不过又没有用这种写法

 

XML语言: Codee#14195
<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标签

 

Java语言: Codee#14202
@ElementList (entry = "entry" , required = true , inline = true ) 
public List < entry > entryList ;

 

 

Q:entry参数是什么

 

A:当Java类中的节点名称和xml节点不相对应的时候,需要用entry指定xml文件中的节点名称

 

XML语言: Codee#14197
<root>
   <username>aaa</username>
   <password>bbb</password>
   <abc key="Key0" value="Value0"/>
   <abc key="Key1" value="Value1"/>
   <abc key="Key2" value="Value2"/>
</root>

 

Java类中对应为

 

Java语言: Codee#14198
@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;
    
}