【JAVA】:Java对象映射XML文件
усил の博客 2020/6/9 Java
# Java对象映射XML文件
# 一、前期准备
- 创建Maven工程
- 导入jar包
# 二、实验
- 在pom.xml中导入jar 包
<dependency>
<groupId>xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.2.2</version>
</dependency>
1
2
3
4
5
2
3
4
5
- 创建实体类
//城市类
public class City {
private String name;//城市名
private String description;//描述;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public City(String name, String description) {
super();
this.name = name;
this.description = description;
}
}
//省份类
public class Provider {
private String name;//省名
private List<City>cities=new ArrayList<City>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
public void addcity(City city){
cities.add(city);
}
}
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
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
- 设置数据
public static List<Provider> getprovinderList(){
Provider p1=new Provider();
p1.setName("北京");
p1.addcity(new City("东城区","dongchneng"));
p1.addcity(new City("昌平区","changping"));
Provider p2=new Provider();
p2.setName("广东省");
p2.addcity(new City("汕头市","shangtou"));
p2.addcity(new City("深圳市","shengzheng"));
List<Provider>provinderList=new ArrayList<Provider>();
provinderList.add(p1);
provinderList.add(p2);
return provinderList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- 测试 测试一
// 默认生成的xml
public static void fun1(){
//获取数据
List<Provider>provinderList=getprovinderList();
//获取XStream实例化对象
XStream x=new XStream();
//生成XML
String str=x.toXML(provinderList);
System.out.println(str);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
测试二
// 起别名
public static void fun2(){
List<Provider>provinderList=getprovinderList();
XStream x=new XStream();
//给List类型指定别名为china
x.alias("china", List.class);
x.alias("provider", Provider.class);
x.alias("city", City.class);
String str=x.toXML(provinderList);
System.out.println(str);
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
测试三
public static void fun3(){
List<Provider>provinderList=getprovinderList();
XStream x=new XStream();
x.alias("china", List.class);//给List类型指定别名为china
x.alias("provider", Provider.class);
x.alias("city", City.class);
x.useAttributeFor(Provider.class,"name");// 把Provider类型的name属性,生成<Provider>元素的属性
x.addImplicitCollection(Provider.class, "cities");//去除<cities>这样的Collection类型的属性
String str=x.toXML(provinderList);
System.out.println(str);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
测试四
// 去除不要的javabean属性
public static String fun4(){
List<Provider>provinderList=getprovinderList();
XStream x=new XStream();
x.alias("china", List.class);
x.alias("provider", Provider.class);
x.alias("city", City.class);
x.useAttributeFor(Provider.class,"name");
x.addImplicitCollection(Provider.class, "cities");
x.omitField(City.class, "description");
String str=x.toXML(provinderList);
return str;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16