使用限制: JDK版本不能<1.5.
虽然预处理注解是安全的,但自动侦查注解可能发生竞争条件.
特点:
简化的API;
无映射文件;
高性能,低内存占用;
整洁的XML;
不需要修改对象;支持内部私有字段,不需要setter/getter方法,final字段;非公有类,内部类;类不需要默认构造器,完全对象图支持.维护对象引用计数,循环引用. i
提供序列化接口;
自定义转换类型策略;
详细的错误诊断;
快速输出格式;当前支持 JSON 和 morphing.
使用场景
Transport 转换
Persistence 持久化对象
Configuration 配置
Unit Tests 单元测
隐式集合
当我们使用集合类时不想显示集合,只显示里面的元素即可.
使用隐式集合前:
使用隐式集合:
xstream.addImplicitCollection(Person.class, "list");
使用隐式集合后:
import
java.util.List;
import
com.thoughtworks.xstream.annotations.XStreamAlias;
import
com.thoughtworks.xstream.annotations.XStreamImplicit;
/**
*@ClassName:PersonBean
*@author: chenyoulong Email: [email protected]
*@date :2012-9-28 下午3:10:47
*@Description:TODO
*/
@XStreamAlias
(
"person"
)
public
class
PersonBean {
@XStreamAlias
(
"firstName"
)
private
String firstName;
@XStreamAlias
(
"lastName"
)
private
String lastName;
@XStreamAlias
(
"telphone"
)
private
PhoneNumber tel;
@XStreamAlias
(
"faxphone"
)
private
PhoneNumber fax;
//测试一个标签下有多个同名标签
@XStreamAlias
(
"friends"
)
private
Friends friend;
//测试一个标签下循环对象
@XStreamAlias
(
"pets"
)
private
Pets pet;
//省略setter和getter
}
|
1
2
3
4
5
6
7
8
9
10
|
@XStreamAlias
(
"phoneNumber"
)
public
class
PhoneNumber{
@XStreamAlias
(
"code"
)
private
int
code;
@XStreamAlias
(
"number"
)
private
String number;
//省略setter和getter
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/**
* 用Xstream注解的方式实现:一个标签下有多个同名标签
*@ClassName:Friends
*@author: chenyoulong Email: [email protected]
*@date :2012-9-28 下午4:32:24
*@Description:TODO 5个name 中国,美国,俄罗斯,英国,法国
*http://blog.csdn.net/menhuanxiyou/article/details/5426765
*/
public
static
class
Friends{
@XStreamImplicit
(itemFieldName=
"name"
)
//itemFieldName定义重复字段的名称,
/*
*/
private
List
public
List
return
name;
}
public
void
setName(List
this
.name = name;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//测试同一标签下循环某一对象
public
class
Animal{
@XStreamAlias
(
"name"
)
private
String name;
@XStreamAlias
(
"age"
)
private
int
age;
public
Animal(String name,
int
age){
this
.name=name;
this
.age=age;
}
//省略setter和getter
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/**
* 测试同一标签下循环某一对象
*@ClassName:Pets
*@author: chenyoulong Email: [email protected]
*@date :2012-9-28 下午6:26:01
*@Description:TODO
*/
public
class
Pets{
@XStreamImplicit
(itemFieldName=
"pet"
)
private
List
public
List
return
animalList;
}
public
void
setAnimalList(List
this
.animalList = animalList;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import
com.thoughtworks.xstream.XStream;
import
com.thoughtworks.xstream.io.json.JsonWriter.Format;
import
com.thoughtworks.xstream.io.xml.DomDriver;
/**
*@ClassName:PersonTest
*@author: chenyoulong
*@date :2012-9-28 下午3:25:09
*@Description:TODO
*/
public
class
PersonTest {
/**
* @Title: main
* @Description: TODO
* @param args
* @return void
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
PersonBean per=
new
PersonBean();
per.setFirstName(
"chen"
);
per.setLastName(
"youlong"
);
PhoneNumber tel=
new
PhoneNumber();
tel.setCode(
137280
);
tel.setNumber(
"137280968"
);
PhoneNumber fax=
new
PhoneNumber();
fax.setCode(
20
);
fax.setNumber(
"020221327"
);
per.setTel(tel);
per.setFax(fax);
//测试一个标签下有多个同名标签
List
new
ArrayList
friendList.add(
"A1"
);
friendList.add(
"A2"
);
friendList.add(
"A3"
);
Friends friend1=
new
Friends();
friend1.setName(friendList);
per.setFriend(friend1);
//测试一个标签下循环对象
Animal dog=
new
Animal(
"Dolly"
,
2
);
Animal cat=
new
Animal(
"Ketty"
,
2
);
List
new
ArrayList
petList.add(dog);
petList.add(cat);
Pets pet=
new
Pets();
pet.setAnimalList(petList);
per.setPet(pet);
//java对象转换成xml
String xml=XmlUtil.toXml(per);
System.out.println(
"xml==="
+xml);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
xml===
137280968
020221327
2
2
|