简单使用XStream
Stream
1.什么作用
*可以把JavaBean转换为(序列化为)xml
2.XStream的jar
*核心jar:xstream-1.4.7.jar
*必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的xml解析器)
3、使用步骤
*XStream xstream=new XStream();
*String xmlStr=xstream.toXML(javabean);
4、使用细节:
*别名,把类型对应的元素名修改了
>xstream.alias("china",List.class):让List类型生成的元素名为china
>xstream.alias("province",Province.class):让Province类型生成的元素名为province
*使用为属性:默认类的成员,生成的是元素的子元素!我们希望让类的成员生成属性
>xstream.useAttributeFor(Province.class,"name"):把Province类的名为name成员,生成<province>元素的属性
*去除Collection类型的成员:我们只需要Collection的内容,而不需要Collection本身也生成一个元素
>xstream.addImplicitCollection(Province.class,"cities");:让Province类的名为cities(它是List类型,它的内容还会生成元素)的属性不生成元素
*去除类的指定成名,让其不生成xml元素
>xstream.omitField(City.class,"description"):在生成的xml中不会生成City类中的名为description的元素
package cn.itcast.demo1; 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 City() { super(); } @Override public String toString() { return "City [name=" + name + ", description=" + description + "]"; } }
package cn.itcast.demo1; import java.util.ArrayList; import java.util.List; public class Province { 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 Province(String name, List<City> cities) { super(); this.name = name; this.cities = cities; } public Province() { super(); // TODO Auto-generated constructor stub } public void addCity(City city){ cities.add(city); } }
package cn.itcast.demo1; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.thoughtworks.xstream.XStream; public class Demo1 { public List<Province> getProvinceList(){ Province p1=new Province(); p1.setName("北京"); p1.addCity(new City("东城区","DongChengQu")); p1.addCity(new City("昌平区","ChangPingQu")); Province p2=new Province(); p2.setName("辽宁"); p2.addCity(new City("沈阳","shenYang")); p2.addCity(new City("葫芦岛","huLuDao")); List<Province> provinceList=new ArrayList<Province>(); provinceList.add(p1); provinceList.add(p2); return provinceList; } /* <list> <cn.itcast.demo1.Province> <name>北京</name> <cities> <cn.itcast.demo1.City> <name>东城区</name> <description>DongChengQu</description> </cn.itcast.demo1.City> <cn.itcast.demo1.City> <name>昌平区</name> <description>ChangPingQu</description> </cn.itcast.demo1.City> </cities> </cn.itcast.demo1.Province> <cn.itcast.demo1.Province> <name>辽宁</name> <cities> <cn.itcast.demo1.City> <name>沈阳</name> <description>shenYang</description> </cn.itcast.demo1.City> <cn.itcast.demo1.City> <name>葫芦岛</name> <description>huLuDao</description> </cn.itcast.demo1.City> </cities> </cn.itcast.demo1.Province> </list> */ @Test public void fun1(){ List<Province> proList=getProvinceList(); /* * 创建XStream对象 * 调用toXML将集合转换成字符串对象 */ XStream xstream=new XStream(); String s=xstream.toXML(proList); System.out.println(s); } /* * 别名(alias) * 希望: * 默认List类型对应<list>元素,让List类型对应<china>元素 * 默认Province类型对应<cn.itcast.demo1.province>,希望它对应<province> * 默认City类型对应<cn.itcast.demo1.City>,希望它对应<city> */ /* <china> <province> <name>北京</name> <cities> <city> <name>东城区</name> <description>DongChengQu</description> </city> <city> <name>昌平区</name> <description>ChangPingQu</description> </city> </cities> </province> <province> <name>辽宁</name> <cities> <city> <name>沈阳</name> <description>shenYang</description> </city> <city> <name>葫芦岛</name> <description>huLuDao</description> </city> </cities> </province> </china> */ @Test public void fun2(){ List<Province> proList=getProvinceList(); XStream xstream=new XStream(); /* * 给指定的类型指定别名 */ xstream.alias("china",List.class); xstream.alias("province", Province.class); xstream.alias("city", City.class); String s=xstream.toXML(proList); System.out.println(s); } /* * 默认javabean的属性都会生成子元素,而现在希望生成元素的属性 */ /* <china> <province name="北京"> <cities> <city> <name>东城区</name> <description>DongChengQu</description> </city> <city> <name>昌平区</name> <description>ChangPingQu</description> </city> </cities> </province> <province name="辽宁"> <cities> <city> <name>沈阳</name> <description>shenYang</description> </city> <city> <name>葫芦岛</name> <description>huLuDao</description> </city> </cities> </province> </china> */ @Test public void fun3(){ List<Province> proList=getProvinceList(); XStream xstream=new XStream(); xstream.alias("china", List.class); xstream.alias("province", Province.class); xstream.alias("city", City.class); /* * 把Province类型的name属性生成province元素的属性 */ xstream.useAttributeFor(Province.class, "name"); String s=xstream.toXML(proList); System.out.println(s); } /* <china> <province name="北京"> <city> <name>东城区</name> <description>DongChengQu</description> </city> <city> <name>昌平区</name> <description>ChangPingQu</description> </city> </province> <province name="辽宁"> <city> <name>沈阳</name> <description>shenYang</description> </city> <city> <name>葫芦岛</name> <description>huLuDao</description> </city> </province> </china> */ /* * 去除List类型的属性,只把list中的元素生成xml元素 */ @Test public void fun4(){ List<Province> proList=getProvinceList(); XStream xstream=new XStream(); xstream.alias("china", List.class); xstream.alias("province", Province.class); xstream.alias("city", City.class); xstream.useAttributeFor(Province.class, "name"); /* * 去除<cities>这样的Collection类型的属性 * 去除Province类的名为cities的List类型的属性 */ xstream.addImplicitCollection(Province.class, "cities"); String s=xstream.toXML(proList); System.out.println(s); } /* <china> <province name="北京"> <city> <name>东城区</name> </city> <city> <name>昌平区</name> </city> </province> <province name="辽宁"> <city> <name>沈阳</name> </city> <city> <name>葫芦岛</name> </city> </province> </china> */ /* * 去除不想要的javabean属性 * 就是让某些javabean属性不生成xml元素 */ @Test public void fun5(){ List<Province> proList=getProvinceList(); XStream xstream=new XStream(); xstream.alias("china", List.class); xstream.alias("province", Province.class); xstream.alias("city", City.class); xstream.useAttributeFor(Province.class, "name"); xstream.addImplicitCollection(Province.class, "cities"); /* * 让City类的,名为description属性不生成对应的xml元素 */ xstream.omitField(City.class, "description"); String s=xstream.toXML(proList); System.out.println(s); } }