XStream可以把JavaBean对象转换成XML,通常服务器向客户端响应的数据都是来自数据库的一组对象,而我们不能直接把对象响应给响应端,所以我们需要把对象转换成XML再响应给客户端,这时就需要XStream。
也就是说,XStream是一门将对象转换成XML的技术。
XStream相关jar包:
● 核心JAR包:xstream-1.4.7.jar;
● 必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器)。
先写两个Province和City类:
<span style="font-size:18px;">Province.java public class Province { private String name; private List<City> cities = new ArrayList<City>(); public void addCity(City city) { cities.add(city); } …… }</span>
<span style="font-size:18px;">City.java public class City { private String name; private String description; …… }</span>
接下来,我们需要写一个main(),创建一个List,List中存放两个Province对象!最终我们把List转换成XML。
<span style="font-size:18px;"> Province p1 = new Province("辽宁省"); p1.addCity(new City("沈阳", "shenyang")); p1.addCity(new City("大连", "dalian")); Province p2 = new Province("吉林省"); p2.addCity(new City("长春", "changchen")); p2.addCity(new City("白城", "baicheng")); List<Province> list = new ArrayList<Province>(); list.add(p1); list.add(p2);</span>
下面是使用XStream转换list为XML的代码:
<span style="font-size:18px;"> XStream xstream = new XStream(); String s = xstream.toXML(list); System.out.println(s);</span>
转换结果为:
<span style="font-size:18px;"><list> <cn.xstream.demo1.Province> <name>辽宁省</name> <cities> <cn.xstream.demo1.City> <name>沈阳</name> <description>shenyang</description> </cn.itcast.xstream.demo1.City> <cn.xstream.demo1.City> <name>大连</name> <description>dalian</description> </cn.xstream.demo1.City> </cities> </cn.xstream.demo1.Province> <cn.xstream.demo1.Province> <name>吉林省</name> <cities> <cn.xstream.demo1.City> <name>长春</name> <description>changchen</description> </cn.xstream.demo1.City> <cn.xstream.demo1.City> <name>白城</name> <description>baicheng</description> </cn.xstream.demo1.City> </cities> </cn.xstream.demo1.Province> </list> </span>
也就是说,XStream是根据对象名、类名、属性名来生成XML文档的。
生成的XML中,与类名对应的元素名称包含了包名部分,若想自定义生成的元素名称,需要使用XStream为类名提供别名:
<span style="font-size:18px;"> xstream.alias("province", Province.class); xstream.alias("china", List.class); xstream.alias("city", City.class); </span>
<span style="font-size:18px;"><china> <province> <name>辽宁省</name> <cities> <city> <name>沈阳</name> <description>shenyang</description> </city> <city> <name>大连</name> <description>dalian</description> </city> </cities> </province> <province> <name>吉林省</name> <cities> <city> <name>长春</name> <description>changchen</description> </city> <city> <name>白城</name> <description>baicheng</description> </city> </cities> </province> </china></span>
例如我们需要把<province>子元素<name>变成:<province name=””>样式,那么需要调用如下方法:
<span style="font-size:18px;">xstream.useAttributeFor(Province.class, "name");</span>
结果为:
<span style="font-size:18px;"><china> <province name="辽宁省"> <cities> <city> <name>沈阳</name> <description>shenyang</description> </city> <city> <name>大连</name> <description>dalian</description> </city> </cities> </province> <province name="吉林省"> <cities> <city> <name>长春</name> <description>changchen</description> </city> <city> <name>白城</name> <description>baicheng</description> </city> </cities> </province> </china></span>
因为Pronvice类有一个cities成员,所以生成了<cities>元素,但这个元素对XML文档而言没有什么意义,所以我们希望把它去除:
<span style="font-size:18px;">xstream.addImplicitCollection(Province.class, "cities");</span>
结果为:
<span style="font-size:18px;"><china> <province name="辽宁省"> <city> <name>沈阳</name> <description>shenyang</description> </city> <city> <name>大连</name> <description>dalian</description> </city> </province> <province name="吉林省"> <city> <name>长春</name> <description>changchen</description> </city> <city> <name>白城</name> <description>baicheng</description> </city> </province> </china></span>
到现在为止,我们都是每个类,每个成员都有对应的元素(或属性)存在,但有时我们并不希望某些类的成员在对应的XML文档中出现,例如我们不希望City类的description成员出现在XML文档中,可以使用下面方法:
<span style="font-size:18px;">xstream.omitField(City.class, "description"); </span>
结果为:
<span style="font-size:18px;"><china> <province name="辽宁省"> <city> <name>沈阳</name> </city> <city> <name>大连</name> </city> </province> <province name="吉林省"> <city> <name>长春</name> </city> <city> <name>白城</name> </city> </province> </china></span>