首页 > 代码库 > XStream-----把JavaBean转换为xml的工具

XStream-----把JavaBean转换为xml的工具

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的对应的元素!

应用实例:

  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输出结果:

技术分享
 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输出结果:

技术分享
 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输出结果:

技术分享
 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

XStream-----把JavaBean转换为xml的工具