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. package cn.itcast.demo1;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.junit.Test;  
  7.   
  8. import com.thoughtworks.xstream.XStream;  
  9.   
  10. /** 
  11.  * 演示XStream 
  12.  * @author cxf 
  13.  * 
  14.  */  
  15. public class Demo1 {  
  16.     // 返回javabean集合  
  17.     public List<Province> getProinvceList() {  
  18.         Province p1 = new Province();  
  19.         p1.setName("北京");  
  20.         p1.addCity(new City("東城區""DongChengQu"));  
  21.         p1.addCity(new City("昌平區""ChangPingQu"));  
  22.           
  23.         Province p2 = new Province();  
  24.         p2.setName("遼寧");  
  25.         p2.addCity(new City("瀋陽""shenYang"));  
  26.         p2.addCity(new City("葫蘆島""huLuDao"));  
  27.           
  28.         List<Province> provinceList = new ArrayList<Province>();  
  29.         provinceList.add(p1);  
  30.         provinceList.add(p2);  
  31.           
  32.         return provinceList;  
  33.     }  
  34.       
  35.     /** 
  36. <list> --> List類型顯示list 
  37.   <cn.itcast.demo1.Province> --> javabean的類型爲Province,它元素的名稱爲類的完整名 
  38.     <name>北京</name> --> javabean的屬性名 
  39.     <cities> --> javabean的屬性名 
  40.       <cn.itcast.demo1.City> --> 類名 
  41.         <name>東城區</name> --> 屬性名 
  42.         <description>DongChengQu</description> --> 屬性名 
  43.       </cn.itcast.demo1.City> 
  44.       <cn.itcast.demo1.City> 
  45.         <name>昌平區</name> 
  46.         <description>ChangPingQu</description> 
  47.       </cn.itcast.demo1.City> 
  48.     </cities> 
  49.   </cn.itcast.demo1.Province> 
  50.   <cn.itcast.demo1.Province> 
  51.     <name>遼寧</name> 
  52.     <cities> 
  53.       <cn.itcast.demo1.City> 
  54.         <name>瀋陽</name> 
  55.         <description>shenYang</description> 
  56.       </cn.itcast.demo1.City> 
  57.       <cn.itcast.demo1.City> 
  58.         <name>葫蘆島</name> 
  59.         <description>huLuDao</description> 
  60.       </cn.itcast.demo1.City> 
  61.     </cities> 
  62.   </cn.itcast.demo1.Province> 
  63. </list> 
  64.      */  
  65.     @Test  
  66.     public void fun1() {  
  67.         List<Province> proList = getProinvceList();  
  68.         /* 
  69.          * 創建XStream對象 
  70.          * 調用toXML把集合轉換成xml字符串 
  71.          */  
  72.         XStream xstream = new XStream();  
  73.         String s = xstream.toXML(proList);  
  74.         System.out.println(s);  
  75.     }  
  76.       
  77.     /* 
  78.      * 別名(alias) 
  79.      * 希望: 
  80.      * * 默認List類型對應<list>元素,希望讓List類型對應<china>元素 
  81.      * * 默認Province類型對應<cn.itcast.demo1.Province>,希望讓它對應<province> 
  82.      * * 默認City類型對應<cn.itcast.demo1.City>,希望它對應<city>元素 
  83.      */  
  84.     /* 
  85. <china> 
  86.   <province> 
  87.     <name>北京</name> 
  88.     <cities> 
  89.       <city> 
  90.         <name>東城區</name> 
  91.         <description>DongChengQu</description> 
  92.       </city> 
  93.       <city> 
  94.         <name>昌平區</name> 
  95.         <description>ChangPingQu</description> 
  96.       </city> 
  97.     </cities> 
  98.   </province> 
  99.   <province> 
  100.     <name>遼寧</name> 
  101.     <cities> 
  102.       <city> 
  103.         <name>瀋陽</name> 
  104.         <description>shenYang</description> 
  105.       </city> 
  106.       <city> 
  107.         <name>葫蘆島</name> 
  108.         <description>huLuDao</description> 
  109.       </city> 
  110.     </cities> 
  111.   </province> 
  112. </china> 
  113.      */  
  114.     @Test  
  115.     public void fun2() {  
  116.         List<Province> proList = getProinvceList();  
  117.         XStream xstream = new XStream();  
  118.         /* 
  119.          * 給指定的類型指定別名 
  120.          */  
  121.         xstream.alias("china", List.class);//給List類型指定別名爲china  
  122.         xstream.alias("province", Province.class);//給Province指定別名爲province  
  123.         xstream.alias("city", City.class);//給City類型指定別名爲city  
  124.           
  125.           
  126.         String s = xstream.toXML(proList);  
  127.         System.out.println(s);  
  128.     }  
  129.       
  130.     /* 
  131.      * 默認javabean的屬性都會生成子元素,而現在希望生成元素的屬性 
  132.      */  
  133. /* 
  134. <china> 
  135.   <province name="北京"> 
  136.     <cities> 
  137.       <city> 
  138.         <name>東城區</name> 
  139.         <description>DongChengQu</description> 
  140.       </city> 
  141.       <city> 
  142.         <name>昌平區</name> 
  143.         <description>ChangPingQu</description> 
  144.       </city> 
  145.     </cities> 
  146.   </province> 
  147.   <province name="遼寧"> 
  148.     <cities> 
  149.       <city> 
  150.         <name>瀋陽</name> 
  151.         <description>shenYang</description> 
  152.       </city> 
  153.       <city> 
  154.         <name>葫蘆島</name> 
  155.         <description>huLuDao</description> 
  156.       </city> 
  157.     </cities> 
  158.   </province> 
  159.  */  
  160.     @Test  
  161.     public void fun3() {  
  162.         List<Province> proList = getProinvceList();  
  163.         XStream xstream = new XStream();  
  164.         xstream.alias("china", List.class);//給List類型指定別名爲china  
  165.         xstream.alias("province", Province.class);//給Province指定別名爲province  
  166.         xstream.alias("city", City.class);//給City類型指定別名爲city  
  167.           
  168.           
  169.         /* 
  170.          * 把Province類型的name屬性,生成<province>元素的屬性 
  171.          */  
  172.         xstream.useAttributeFor(Province.class"name");  
  173.           
  174.           
  175.         String s = xstream.toXML(proList);  
  176.         System.out.println(s);        
  177.     }  
  178.   
  179.       
  180.     /* 
  181.      * 去除List類型的屬性,只把list中的元素生成xml元素 
  182.      */  
  183. /* 
  184. <china> 
  185.   <province name="北京"> 
  186.     <city> 
  187.       <name>東城區</name> 
  188.       <description>DongChengQu</description> 
  189.     </city> 
  190.     <city> 
  191.       <name>昌平區</name> 
  192.       <description>ChangPingQu</description> 
  193.     </city> 
  194.   </province> 
  195.   <province name="遼寧"> 
  196.     <city> 
  197.       <name>瀋陽</name> 
  198.       <description>shenYang</description> 
  199.     </city> 
  200.     <city> 
  201.       <name>葫蘆島</name> 
  202.       <description>huLuDao</description> 
  203.     </city> 
  204.   </province> 
  205. </china> 
  206.  */  
  207.     @Test  
  208.     public void fun4() {  
  209.         List<Province> proList = getProinvceList();  
  210.         XStream xstream = new XStream();  
  211.         xstream.alias("china", List.class);//給List類型指定別名爲china  
  212.         xstream.alias("province", Province.class);//給Province指定別名爲province  
  213.         xstream.alias("city", City.class);//給City類型指定別名爲city  
  214.         xstream.useAttributeFor(Province.class"name");//把Province類型的name屬性,生成<province>元素的屬性  
  215.           
  216.           
  217.         /* 
  218.          * 去除<cities>這樣的Collection類型的屬性 
  219.          * 去除Provice類的名爲cities的List類型的屬性! 
  220.          */  
  221.         xstream.addImplicitCollection(Province.class"cities");  
  222.           
  223.           
  224.         String s = xstream.toXML(proList);  
  225.         System.out.println(s);        
  226.     }  
  227.       
  228.     /** 
  229.      * 去除不想要的javabean屬性 
  230.      * 就是讓某引起javabean屬性,不生成對應的xml元素! 
  231.      */  
  232. /* 
  233. <china> 
  234.   <province name="北京"> 
  235.     <city> 
  236.       <name>東城區</name> 
  237.     </city> 
  238.     <city> 
  239.       <name>昌平區</name> 
  240.     </city> 
  241.   </province> 
  242.   <province name="遼寧"> 
  243.     <city> 
  244.       <name>瀋陽</name> 
  245.     </city> 
  246.     <city> 
  247.       <name>葫蘆島</name> 
  248.     </city> 
  249.   </province> 
  250. </china> 
  251.  */  
  252.     @Test  
  253.     public void fun5() {  
  254.         List<Province> proList = getProinvceList();  
  255.         XStream xstream = new XStream();  
  256.         xstream.alias("china", List.class);//給List類型指定別名爲china  
  257.         xstream.alias("province", Province.class);//給Province指定別名爲province  
  258.         xstream.alias("city", City.class);//給City類型指定別名爲city  
  259.         xstream.useAttributeFor(Province.class"name");//把Province類型的name屬性,生成<province>元素的屬性  
  260.         xstream.addImplicitCollection(Province.class"cities");//去除Provice類的名爲cities的List類型的屬性!  
  261.           
  262.           
  263.         /* 
  264.          * 讓City類的,名爲description屬性不生成對應的xml元素 
  265.          */  
  266.         xstream.omitField(City.class"description");  
  267.           
  268.           
  269.         String s = xstream.toXML(proList);  
  270.         System.out.println(s);        
  271.     }  






文章轉自:http://blog.csdn.net/JOKER_SAMA/article/details/61197689

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章