1. 什麼作用
* 可以把JavaBean轉換爲(序列化爲)xml
* 核心JAR包:xstream-1.4.7.jar;
* 必須依賴包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);
* XStream xstream = new XStream();
* String xmlStr = xstream.toXML(javabean);
* 別名:把類型對應的元素名修改了
> 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 cn.itcast.demo1;
- import java.util.ArrayList;
- import java.util.List;
- import org.junit.Test;
- import com.thoughtworks.xstream.XStream;
- /**
- * 演示XStream
- * @author cxf
- *
- */
- public class Demo1 {
- // 返回javabean集合
- public List<Province> getProinvceList() {
- 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> --> List類型顯示list
- <cn.itcast.demo1.Province> --> javabean的類型爲Province,它元素的名稱爲類的完整名
- <name>北京</name> --> javabean的屬性名
- <cities> --> javabean的屬性名
- <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 = getProinvceList();
- /*
- * 創建XStream對象
- * 調用toXML把集合轉換成xml字符串
- */
- 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 = getProinvceList();
- XStream xstream = new XStream();
- /*
- * 給指定的類型指定別名
- */
- xstream.alias("china", List.class);//給List類型指定別名爲china
- xstream.alias("province", Province.class);//給Province指定別名爲province
- xstream.alias("city", City.class);//給City類型指定別名爲city
- 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>
- */
- @Test
- public void fun3() {
- List<Province> proList = getProinvceList();
- XStream xstream = new XStream();
- xstream.alias("china", List.class);//給List類型指定別名爲china
- xstream.alias("province", Province.class);//給Province指定別名爲province
- xstream.alias("city", City.class);//給City類型指定別名爲city
- /*
- * 把Province類型的name屬性,生成<province>元素的屬性
- */
- xstream.useAttributeFor(Province.class, "name");
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- /*
- * 去除List類型的屬性,只把list中的元素生成xml元素
- */
- /*
- <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>
- */
- @Test
- public void fun4() {
- List<Province> proList = getProinvceList();
- XStream xstream = new XStream();
- xstream.alias("china", List.class);//給List類型指定別名爲china
- xstream.alias("province", Province.class);//給Province指定別名爲province
- xstream.alias("city", City.class);//給City類型指定別名爲city
- xstream.useAttributeFor(Province.class, "name");//把Province類型的name屬性,生成<province>元素的屬性
- /*
- * 去除<cities>這樣的Collection類型的屬性
- * 去除Provice類的名爲cities的List類型的屬性!
- */
- xstream.addImplicitCollection(Province.class, "cities");
- String s = xstream.toXML(proList);
- 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> proList = getProinvceList();
- XStream xstream = new XStream();
- xstream.alias("china", List.class);//給List類型指定別名爲china
- xstream.alias("province", Province.class);//給Province指定別名爲province
- xstream.alias("city", City.class);//給City類型指定別名爲city
- xstream.useAttributeFor(Province.class, "name");//把Province類型的name屬性,生成<province>元素的屬性
- xstream.addImplicitCollection(Province.class, "cities");//去除Provice類的名爲cities的List類型的屬性!
- /*
- * 讓City類的,名爲description屬性不生成對應的xml元素
- */
- xstream.omitField(City.class, "description");
- String s = xstream.toXML(proList);
- System.out.println(s);
- }
- }
文章轉自:http://blog.csdn.net/JOKER_SAMA/article/details/61197689