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>元素的name属性
* 去除Collection类型的成名:我们只需要Collection的内容,而不希望Collection本身也生成一个元素
> xstream.addImplicitCollection(Province.class,"cities"):让Province类的名为cities(它是List类型的,它的内容还会生成元素)的成名不生成元素
* 去除类的指定成名,让其不生成xml元素
> xstream.omitField(City.class,"description"):在生成的xml中不会出现City类的名为description的对应的元素!
package 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(); // TODO Auto-generated constructor stub } }
package 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 void addCity(City city){ this.cities.add(city); } public Province(String name) { super(); this.name = name; } }
package demo1; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.thoughtworks.xstream.XStream; public class Demo { public List<Province> getProinvceList(){ Province p1=new Province("北京"); p1.addCity(new City("朝阳区","chaoyang")); p1.addCity(new City("西城区","xiCheng")); Province p2=new Province("内蒙古"); p2.addCity(new City("呼和浩特","huhehaote")); p2.addCity(new City("包头","baotou")); List<Province> list=new ArrayList<Province>(); list.add(p1); list.add(p2); return list; } @Test public void fun1(){ List<Province> provinces=getProinvceList(); /* * 创建XStream对象 * 调用toXML把集合转换成xml字符串 */ XStream xstream=new XStream(); System.out.println(xstream.toXML(provinces)); } /* * 别名(alias) * 希望: * * 默认List类型对应<list>元素,希望让List类型对应<china>元素 * * 默认Province类型对应<demo1.Province>,希望让它对应<province> * * 默认City类型对应<demo1.City>,希望它对应<city>元素 */ @Test public void fun2(){ List<Province> provinces=getProinvceList(); /* * 创建XStream对象 * 调用toXML把集合转换成xml字符串 */ XStream xstream=new XStream(); xstream.alias("china",List.class); xstream.alias("province",Province.class); xstream.alias("city",City.class); System.out.println(xstream.toXML(provinces)); } @Test public void fun3(){ List<Province> provinces=getProinvceList(); /* * 创建XStream对象 * 调用toXML把集合转换成xml字符串 */ XStream xstream=new XStream(); xstream.alias("china",City.class); /* * 把Province类型的name属性,生成<province>元素的属性 */ xstream.useAttributeFor(Province.class,"name"); System.out.println(xstream.toXML(provinces)); } /* * 去掉List类型的属性 */ @Test public void fun4(){ List<Province> provinces=getProinvceList(); XStream xstream=new XStream(); xstream.alias("china",City.class); xstream.useAttributeFor(Province.class,"name"); /* * 去掉<cities>这样的Collection属性 */ xstream.addImplicitCollection(Province.class,"cities"); System.out.println(xstream.toXML(provinces)); } /* * /** * 去除不想要的javabean属性,不生成对应的xml元素! */ @Test public void fun5(){ List<Province> provinces=getProinvceList(); XStream xstream=new XStream(); xstream.alias("china","name"); xstream.addImplicitCollection(Province.class,"cities"); /* * 让City类的,名为description属性不生成对应的xml元素 */ xstream.omitField(City.class,"description"); System.out.println(xstream.toXML(provinces)); } }原文链接:https://www.f2er.com/ajax/162375.html