*能夠把Javabean轉換成(序列化爲)XMLjava
* 核心JAR包:xstream-1.4.7.jaride
* 必須依賴的包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器)this
* XStream xstream = new XStream();spa
* String xmlStr = xstream.toXML(javabean);code
* 別名:把類型對應的元素名修改了xml
> xstream.alias("china", List.class):讓List類型生成的元素名爲china對象
* 使用爲屬性:默認類的成員,生成的是元素的子元素!咱們但願讓類的成員生成元素的屬性blog
> xstream.useAttributeFor(Province.class, "name"):把Province類的名爲name成員,生成<province>元素的name屬性ip
* 去除Collection類型的成員:咱們只須要Collection的內容,而不但願Collection自己也生成一個元素ci
> xstream.addImplicitCollection(Province.class, "cities"):讓Province類的名爲cities(它是List類型的,它的內容還會生成元素)的成員不生成元素
* 去除類的指定成員,讓其不生成xml元素
> xstream.omitField(City.class, "description"):在生成的xml中不會出現City類的,名爲description的對應的元素!
代碼示例:
Province:
package com.xjs.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) { cities.add(city); } }
City:
package com.xjs.demo1; public class City { private String name;// 市名 private String description;// 描述 public City() { } public City(String name, String description) { super(); this.name = name; this.description = 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 + "]"; } }
Demo1:
package com.xjs.demo1; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.thoughtworks.xstream.XStream; /** * 演示 * @author hp * */ public class Demo1 { //返回JavaBean集合 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; } @Test public void fun1(){ List<Province> proList=getProvinceList(); /* * 建立XStream對象 * 調用toXML把集合轉換成xml字符串 */ XStream xs=new XStream(); String xml = xs.toXML(proList); System.out.println(xml); } /* * 別名 * 但願: * >默認List類型對應<List>元素,但願讓List類型對應<china>元素 * >默認Province類型對應<com.xjs.demo1.Province>,但願它對應<province> * >默認City類型對應<com.xjs.demo1.City>,但願對應<city> */ @Test public void fun2(){ List<Province> proList=getProvinceList(); XStream xs=new XStream(); /* * 給指定的類型指定別名 */ xs.alias("china", List.class);// xs.alias("province", Province.class); xs.alias("city", City.class); String xml = xs.toXML(proList); System.out.println(xml); } /* * 默認JavaBean的屬性都會生成子元素,而如今但願生成元素的屬性 */ @Test public void fun3(){ List<Province> proList=getProvinceList(); XStream xs=new XStream(); xs.alias("china", List.class);//List類型指定別名爲china xs.alias("province", Province.class); xs.alias("city", City.class); /* * 把Province類型的name屬性,生成<province>元素的屬性 */ xs.useAttributeFor(Province.class, "name"); String xml = xs.toXML(proList); System.out.println(xml); } /* * 去除List類型的屬性,只把List中的元素生成XML */ @Test public void fun4(){ List<Province> proList=getProvinceList(); XStream xs=new XStream(); xs.alias("china", List.class);//List類型指定別名爲china xs.alias("province", Province.class); xs.alias("city", City.class); /* * 把Province類型的name屬性,生成<province>元素的屬性 */ xs.useAttributeFor(Province.class, "name"); /* * 去除<cities>這樣Collection類型的屬性 * 去除Province類的名爲cities的List類型的屬性 */ xs.addImplicitCollection(Province.class, "cities"); String xml = xs.toXML(proList); System.out.println(xml); } /* * 去除某個JavaBean中的屬性 */ @Test public void fun5(){ List<Province> proList=getProvinceList(); XStream xs=new XStream(); xs.alias("china", List.class);//List類型指定別名爲china xs.alias("province", Province.class); xs.alias("city", City.class); xs.useAttributeFor(Province.class, "name"); xs.addImplicitCollection(Province.class, "cities"); /* * 讓City類的description屬性不生成對應 的xml元素 */ xs.omitField(City.class, "description"); String xml = xs.toXML(proList); System.out.println(xml); } }