XStream-----把JavaBean轉換爲xml的工具

1. 什麼做用java

  能夠把JavaBean轉換爲(序列化爲)xmlide

2. XStream的jar包
  核心JAR包:xstream-1.4.7.jar;
  必須依賴包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);this

3. 使用步驟
  XStream xstream = new XStream();
  String xmlStr = xstream.toXML(javabean);spa

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的對應的元素!3d

應用實例:code

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 import org.junit.Test;
  4 import com.thoughtworks.xstream.XStream;
  5 /**
  6  * 演示XStream
  7  */
  8 public class Demo1 {
  9     // 返回javabean集合
 10     public List<Province> getProinvceList() {
 11         Province p1 = new Province();
 12         p1.setName("北京");
 13         p1.addCity(new City("東城區", "DongChengQu"));
 14         p1.addCity(new City("昌平區", "ChangPingQu"));
 15         Province p2 = new Province();
 16         p2.setName("遼寧");
 17         p2.addCity(new City("瀋陽", "shenYang"));
 18         p2.addCity(new City("葫蘆島", "huLuDao"));
 19         List<Province> provinceList = new ArrayList<Province>();
 20         provinceList.add(p1);
 21         provinceList.add(p2);
 22         return provinceList;
 23     }
 24     @Test
 25     public void fun1() {
 26         List<Province> proList = getProinvceList();
 27         /*
 28          * 建立XStream對象
 29          * 調用toXML把集合轉換成xml字符串
 30          */
 31         XStream xstream = new XStream();
 32         String s = xstream.toXML(proList);
 33         System.out.println(s);
 34     }
 35     /*
 36      * 別名(alias)
 37      * 但願:
 38      * * 默認List類型對應<list>元素,但願讓List類型對應<china>元素
 39      * * 默認Province類型對應<cn.itcast.demo1.Province>,但願讓它對應<province>
 40      * * 默認City類型對應<cn.itcast.demo1.City>,但願它對應<city>元素
 41      */
 42     @Test
 43     public void fun2() {
 44         List<Province> proList = getProinvceList();
 45         XStream xstream = new XStream();
 46         /*
 47          * 給指定的類型指定別名
 48          */
 49         xstream.alias("china", List.class);//給List類型指定別名爲china
 50         xstream.alias("province", Province.class);//給Province指定別名爲province
 51         xstream.alias("city", City.class);//給City類型指定別名爲city
 52         String s = xstream.toXML(proList);
 53         System.out.println(s);
 54     }
 55     /*
 56      * 默認javabean的屬性都會生成子元素,而如今但願生成元素的屬性
 57      */
 58     @Test
 59     public void fun3() {
 60         List<Province> proList = getProinvceList();
 61         XStream xstream = new XStream();
 62         xstream.alias("china", List.class);//給List類型指定別名爲china
 63         xstream.alias("province", Province.class);//給Province指定別名爲province
 64         xstream.alias("city", City.class);//給City類型指定別名爲city
 65         /*
 66          * 把Province類型的name屬性,生成<province>元素的屬性
 67          */
 68         xstream.useAttributeFor(Province.class, "name");
 69         String s = xstream.toXML(proList);
 70         System.out.println(s);        
 71     }
 72     /*
 73      * 去除List類型的屬性,只把list中的元素生成xml元素
 74      */
 75     @Test
 76     public void fun4() {
 77         List<Province> proList = getProinvceList();
 78         XStream xstream = new XStream();
 79         xstream.alias("china", List.class);//給List類型指定別名爲china
 80         xstream.alias("province", Province.class);//給Province指定別名爲province
 81         xstream.alias("city", City.class);//給City類型指定別名爲city
 82         xstream.useAttributeFor(Province.class, "name");//把Province類型的name屬性,生成<province>元素的屬性
 83         /*
 84          * 去除<cities>這樣的Collection類型的屬性
 85          * 去除Provice類的名爲cities的List類型的屬性!
 86          */
 87         xstream.addImplicitCollection(Province.class, "cities");
 88         String s = xstream.toXML(proList);
 89         System.out.println(s);        
 90     }
 91     /*
 92      * 去除不想要的javabean屬性
 93      * 就是讓某引發javabean屬性,不生成對應的xml元素!
 94      */
 95     @Test
 96     public void fun5() {
 97         List<Province> proList = getProinvceList();
 98         XStream xstream = new XStream();
 99         xstream.alias("china", List.class);//給List類型指定別名爲china
100         xstream.alias("province", Province.class);//給Province指定別名爲province
101         xstream.alias("city", City.class);//給City類型指定別名爲city
102         xstream.useAttributeFor(Province.class, "name");//把Province類型的name屬性,生成<province>元素的屬性
103         xstream.addImplicitCollection(Province.class, "cities");//去除Provice類的名爲cities的List類型的屬性!
104         /*
105          * 讓City類的,名爲description屬性不生成對應的xml元素
106          */
107         xstream.omitField(City.class, "description");
108         String s = xstream.toXML(proList);
109         System.out.println(s);        
110     }
111 }
 1 public class City {
 2     private String name;//市名
 3     private String description;//描述
 4     public String getName() {
 5         return name;
 6     }
 7     public void setName(String name) {
 8         this.name = name;
 9     }
10     public String getDescription() {
11         return description;
12     }
13     public void setDescription(String description) {
14         this.description = description;
15     }
16     @Override
17     public String toString() {
18         return "City [name=" + name + ", description=" + description + "]";
19     }
20     public City() {
21         super();
22         // TODO Auto-generated constructor stub
23     }
24     public City(String name, String description) {
25         super();
26         this.name = name;
27         this.description = description;
28     }
29 }
City
 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 public class Province {
 5     private String name;// 省名
 6     private List<City> cities = new ArrayList<City>();
 7 
 8     public String getName() {
 9         return name;
10     }
11 
12     public void setName(String name) {
13         this.name = name;
14     }
15 
16     public List<City> getCities() {
17         return cities;
18     }
19 
20     public void setCities(List<City> cities) {
21         this.cities = cities;
22     }
23 
24     public void addCity(City city) {
25         cities.add(city);
26     }
27 }
Province

fun1輸出結果:xml

 1 <list>
 2   <Province>
 3     <name>北京</name>
 4     <cities>
 5       <City>
 6         <name>東城區</name>
 7         <description>DongChengQu</description>
 8       </City>
 9       <City>
10         <name>昌平區</name>
11         <description>ChangPingQu</description>
12       </City>
13     </cities>
14   </Province>
15   <Province>
16     <name>遼寧</name>
17     <cities>
18       <City>
19         <name>瀋陽</name>
20         <description>shenYang</description>
21       </City>
22       <City>
23         <name>葫蘆島</name>
24         <description>huLuDao</description>
25       </City>
26     </cities>
27   </Province>
28 </list>
fun1

fun2輸出結果:對象

 1 <china>
 2   <province>
 3     <name>北京</name>
 4     <cities>
 5       <city>
 6         <name>東城區</name>
 7         <description>DongChengQu</description>
 8       </city>
 9       <city>
10         <name>昌平區</name>
11         <description>ChangPingQu</description>
12       </city>
13     </cities>
14   </province>
15   <province>
16     <name>遼寧</name>
17     <cities>
18       <city>
19         <name>瀋陽</name>
20         <description>shenYang</description>
21       </city>
22       <city>
23         <name>葫蘆島</name>
24         <description>huLuDao</description>
25       </city>
26     </cities>
27   </province>
28 </china>
fun2

fun3輸出結果:blog

 1 <china>
 2   <province name="北京">
 3     <cities>
 4       <city>
 5         <name>東城區</name>
 6         <description>DongChengQu</description>
 7       </city>
 8       <city>
 9         <name>昌平區</name>
10         <description>ChangPingQu</description>
11       </city>
12     </cities>
13   </province>
14   <province name="遼寧">
15     <cities>
16       <city>
17         <name>瀋陽</name>
18         <description>shenYang</description>
19       </city>
20       <city>
21         <name>葫蘆島</name>
22         <description>huLuDao</description>
23       </city>
24     </cities>
25   </province>
26 </china>
fun3

fun4輸出結果:ip

 1 <china>
 2   <province name="北京">
 3     <city>
 4       <name>東城區</name>
 5       <description>DongChengQu</description>
 6     </city>
 7     <city>
 8       <name>昌平區</name>
 9       <description>ChangPingQu</description>
10     </city>
11   </province>
12   <province name="遼寧">
13     <city>
14       <name>瀋陽</name>
15       <description>shenYang</description>
16     </city>
17     <city>
18       <name>葫蘆島</name>
19       <description>huLuDao</description>
20     </city>
21   </province>
22 </china>
fun4

fun5輸出結果:

 1 <china>
 2   <province name="北京">
 3     <city>
 4       <name>東城區</name>
 5     </city>
 6     <city>
 7       <name>昌平區</name>
 8     </city>
 9   </province>
10   <province name="遼寧">
11     <city>
12       <name>瀋陽</name>
13     </city>
14     <city>
15       <name>葫蘆島</name>
16     </city>
17   </province>
18 </china>
fun5
相關文章
相關標籤/搜索