时间:2017-1-1 17:50
1、XStream的作用
可以把JavaBean转换为(序列化)XML。
2、XStream相关jar包
可以到http://xstream.codehaus.org/去下载XStream的安装包
XStream必导jar包:
* 核心jar包:xstream-1.4.7.jar
* 依赖jar包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器)
3、使用步骤
* XStream xStrem = new XStream();
* String xml = xStream.toXML(JavaBean);
4、使用细节
* 修改类型对应的元素名
> xStream.alias("china", List.class);
让List类型生成的元素名为china
> xStream.alias("province", Province.class);
让Province类型生成的元素名为province
* 让类的成员变为XML标签的属性
默认类的成员生成的元素是子元素,我们希望让类的成员生成为标签的属性。
> xStream.useAttributeFor(Province.class, "name")
把Province类的名为name的成员,生成<province>标签的属性:<province name="...">
* 去除无用的集合类型的成员
我们只需要将Collection的内容生成元素,而不希望将Collection本身也生成一个XML元素。
> xStream.addImplicitCollection(Province.class, "cities")
让Province类的名为cities的集合类型的成员不生成元素
cities的类型是List类型的,它的内容还会生成子元素
* 去除指定类的指定成员,让其不生成XML元素
> xStream.omitField(City.class, "description")
在生成的XML中,不会出现City类中名为description对应的元素。
=============================================================================
示例代码:
----------------------------------------------------------------------------------------------------------------------------
Province.class
import java.util.ArrayList;
import java.util.List;
public class Province {
private String name; // 省名
private List<City> cities = new ArrayList<City>();
public void addCity(City city) {
cities.add(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;
}
}
----------------------------------------------------------------------------------------------------------------------------
City.class
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;
}
@Override
public String toString() {
return "City [name=" + name + ", description=" + description + "]";
}
public City(String name, String description) {
super();
this.name = name;
this.description = description;
}
public City() {
super();
}
}
----------------------------------------------------------------------------------------------------------------------------
Demo.class
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
public class Demo {
/*
* 返回JavaBean集合
*/
public List<Province> getProvinceList(){
Province p1 = new Province();
p1.setName("山东");
p1.addCity(new City("潍坊", "WeiFang"));
p1.addCity(new City("青岛", "QingDao"));
Province p2 = new Province();
p2.setName("江苏");
p2.addCity(new City("南京", "NanJing"));
p2.addCity(new City("苏州", "SuZhou"));
List<Province> provinceList = new ArrayList<Province>();
provinceList.add(p1);
provinceList.add(p2);
return provinceList;
}
/*
* 转换XML
*/
/**
<list> -- List类型显示list
<com.wyc.Province> -- JavaBean的类型为Province,它的元素名称为类的完整包名
<name>山东</name> --> JavaBean的属性名
<cities> --> JavaBean的属性名
<com.wyc.City> --> 完整类名
<name>潍坊</name> --> 属性名
<description>WeiFang</description> --> 属性名
</com.wyc.City>
<com.wyc.City>
<name>青岛</name>
<description>QingDao</description>
</com.wyc.City>
</cities>
</com.wyc.Province>
<com.wyc.Province>
<name>江苏</name>
<cities>
<com.wyc.City>
<name>南京</name>
<description>NanJing</description>
</com.wyc.City>
<com.wyc.City>
<name>苏州</name>
<description>SuZhou</description>
</com.wyc.City>
</cities>
</com.wyc.Province>
</list>
*/
@Test
public void fun1(){
List<Province> provinceList = getProvinceList();
/*
* 创建XStream对象
* 调用toXML()方法把集合转换成XML字符串
*/
XStream xStream = new XStream();
String s = xStream.toXML(provinceList);
System.out.println(s);
}
/*
* 修改XML元素名称(alias)
* 1、把默认的List对应的<list>元素修改为<china>元素
* 2、把默认的Province类型对应的<com.wyc.Province>修改为<province>元素
* 3、把默认的City类型对应的<com.wyc.City>修改为<city>元素
*/
/**
<china>
<province>
<name>山东</name>
<cities>
<city>
<name>潍坊</name>
<description>WeiFang</description>
</city>
<city>
<name>青岛</name>
<description>QingDao</description>
</city>
</cities>
</province>
<province>
<name>江苏</name>
<cities>
<city>
<name>南京</name>
<description>NanJing</description>
</city>
<city>
<name>苏州</name>
<description>SuZhou</description>
</city>
</cities>
</province>
</china>
*/
@Test
public void fun2(){
List<Province> provinceList = getProvinceList();
XStream xStream = new XStream();
/*
* 给指定的类型指定别名
*/
xStream.alias("china", List.class); // 给List类型指定别名为china
xStream.alias("province", Province.class); // 给Province指定别名为province
xStream.alias("city", City.class);
String s = xStream.toXML(provinceList);
System.out.println(s);
}
/*
* 默认JavaBean的属性都会生成子元素,而现在希望将JavaBean的成员生成为元素的属性
*/
/**
<china>
<province name="山东">
<cities>
<city>
<name>潍坊</name>
<description>WeiFang</description>
</city>
<city>
<name>青岛</name>
<description>QingDao</description>
</city>
</cities>
</province>
<province name="江苏">
<cities>
<city>
<name>南京</name>
<description>NanJing</description>
</city>
<city>
<name>苏州</name>
<description>SuZhou</description>
</city>
</cities>
</province>
</china>
*/
@Test
public void fun3(){
List<Province> provinceList = getProvinceList();
XStream xStream = new XStream();
xStream.alias("china", List.class); // 给List类型指定别名为china
xStream.alias("province", Province.class); // 给Province指定别名为province
xStream.alias("city", City.class);
/*
* 把Province类型的name属性,修改为<province>元素的标签属性
*/
// 将Province类的name属性应用为标签属性
xStream.useAttributeFor(Province.class, "name");
String s = xStream.toXML(provinceList);
System.out.println(s);
}
/*
* 去除无用的集合属性,只将集合中的元素生成XML元素
*/
/**
<china>
<province name="山东">
<city>
<name>潍坊</name>
<description>WeiFang</description>
</city>
<city>
<name>青岛</name>
<description>QingDao</description>
</city>
</province>
<province name="江苏">
<city>
<name>南京</name>
<description>NanJing</description>
</city>
<city>
<name>苏州</name>
<description>SuZhou</description>
</city>
</province>
</china>
*/
@Test
public void fun4(){
List<Province> provinceList = getProvinceList();
XStream xStream = new XStream();
xStream.alias("china", List.class); // 给List类型指定别名为china
xStream.alias("province", Province.class); // 给Province指定别名为province
xStream.alias("city", City.class);
xStream.useAttributeFor(Province.class, "name"); // 把Province类型的name属性,修改为<province>元素的标签属性
/*
* 去除<cities>等作为属性的集合元素,只获取集合中的值
*/
// 去除Province类的名为cities的List类型的属性
xStream.addImplicitCollection(Province.class, "cities");
String s = xStream.toXML(provinceList);
System.out.println(s);
}
/*
* 去除不想要的JavaBean属性
* 让某些JavaBean属性不生成对应的XML元素
*/
/**
<china>
<province name="山东">
<city>
<name>潍坊</name>
</city>
<city>
<name>青岛</name>
</city>
</province>
<province name="江苏">
<city>
<name>南京</name>
</city>
<city>
<name>苏州</name>
</city>
</province>
</china>
*/
@Test
public void fun5(){
List<Province> provinceList = getProvinceList();
XStream xStream = new XStream();
xStream.alias("china", List.class); // 给List类型指定别名为china
xStream.alias("province", Province.class); // 给Province指定别名为province
xStream.alias("city", City.class);
xStream.useAttributeFor(Province.class, "name"); // 把Province类型的name属性,修改为<province>元素的标签属性
xStream.addImplicitCollection(Province.class, "cities"); // 去除<cities>等作为属性的集合元素,只获取集合中的值
/*
* 让City类的description属性不生成对应的XML元素
*/
xStream.omitField(City.class, "description");
String s = xStream.toXML(provinceList);
System.out.println(s);
}
}
XStream学习笔记