SimpleXml例子
来源:互联网 发布:智能语音聊天软件 编辑:程序博客网 时间:2024/06/07 22:54
一、简单的例子
代码清单1:
一个普通的JavaBean
01
import
org.simpleframework.xml.Attribute;
02
import
org.simpleframework.xml.Element;
03
import
org.simpleframework.xml.Root;
04
@Root
05
public
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
}
01
public
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
}
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
>
读取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
}
目前我正在按照官方文档的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);
1
Object object=serializer.read(Object.
class
, File file);
1
//XML的根元素,一般在类上面声明
2
@Root
1
//对XML属性的标识
2
@Attribute
1
//对XML中元素的标识
2
@Element
1
//对数组对象的标识
2
@ElementArray
(entry=
"add"
)
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>
<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>
<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 ;
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>
<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;
}
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;
}
- SimpleXml例子
- SimpleXML 使用例子
- SimpleXML
- simplexml
- simplexml
- simpleXML
- java simpleXml序列化反序列化小例子
- php SimpleXML读写XML接口文件例子分析
- PHP SimpleXML
- SimpleXML 简介
- simpleXML处理
- SimpleXML 操作
- PHP SimpleXML
- 100. SimpleXML
- PHP SimpleXML
- XML----SimpleXML,SimpleXML AND XPath
- php5中的simplexml
- PHP5的Simplexml
- 神奇的NULL
- 多态
- 黑马程序员_java基础复习之七泛型
- 关于独立游戏开发者营销游戏的建议清单
- windows8安装myeclipse 10.0时出现failed to find a main class in "c:\windows\temp"解决
- SimpleXml例子
- [创业说]一个人的创业时代
- your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found
- c++ builder 获取DBGrid多选数据某个字段的值
- </label>标签使用
- RBM
- js调试之firbug
- 如何释放被占用的COM口?
- Eclipse 安装配置指南