标签:
1 package com.demo; 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 public class XStreamDemo { 11 public List<Province> getProvince(){ 12 Province bj = new Province(); 13 bj.setName("北京"); 14 List<City> bjCitys = new ArrayList<City>(); 15 City dcq = new City("东城区","dongchengqu"); 16 City xcq = new City("西城区","xichengqu"); 17 City bcq = new City("北城区","beichengqu"); 18 bjCitys.add(dcq); 19 bjCitys.add(xcq); 20 bjCitys.add(bcq); 21 bj.setCitys(bjCitys); 22 23 24 Province gd = new Province(); 25 bj.setName("广东"); 26 List<City> gdCitys = new ArrayList<City>(); 27 City gzs = new City("广州市","guangzhoushi"); 28 City szs = new City("深圳市","shenzhenshi"); 29 City czs = new City("潮州市","chaozhoushi"); 30 gdCitys.add(gzs); 31 gdCitys.add(szs); 32 gdCitys.add(czs); 33 gd.setCitys(gdCitys); 34 35 List<Province> provinces = new ArrayList<Province>(); 36 provinces.add(bj); 37 provinces.add(gd); 38 return provinces; 39 } 40 /** 41 * 用xstream把JavaBean转换成xml字符串 42 */ 43 @Test 44 public void fun1(){ 45 XStream xs = new XStream(); 46 String s = xs.toXML(getProvince()); 47 System.out.println(s); 48 } 49 50 51 /** 52 * 使用xstream的alias(String name,Class object);方法更改类的名称 53 */ 54 @Test 55 public void fun2(){ 56 XStream xs = new XStream(); 57 xs.alias("china", List.class); 58 xs.alias("province", Province.class); 59 xs.alias("city", City.class); 60 String s = xs.toXML(getProvince()); 61 System.out.println(s); 62 } 63 64 /** 65 * 把子元素变为元素属性 66 */ 67 @Test 68 public void fun3(){ 69 XStream xs = new XStream(); 70 //更改别名 71 xs.alias("china", List.class); 72 xs.alias("province", Province.class); 73 xs.alias("city", City.class); 74 //把子元素变为元素属性 75 xs.useAttributeFor(Province.class, "name"); 76 77 String s = xs.toXML(getProvince()); 78 System.out.println(s); 79 } 80 /** 81 * 去除集合属性对应元素 82 */ 83 @Test 84 public void fun4(){ 85 XStream xs = new XStream(); 86 //更改别名 87 xs.alias("china", List.class); 88 xs.alias("province", Province.class); 89 xs.alias("city", City.class); 90 //把子元素变为元素属性 91 xs.useAttributeFor(Province.class, "name"); 92 // 去除集合属性对应元素 93 xs.addImplicitCollection(Province.class, "citys"); 94 String s = xs.toXML(getProvince()); 95 System.out.println(s); 96 } 97 /** 98 * 去除集合属性对应元素 99 */ 100 @Test 101 public void fun5(){ 102 XStream xs = new XStream(); 103 //更改别名 104 xs.alias("china", List.class); 105 xs.alias("province", Province.class); 106 xs.alias("city", City.class); 107 //把子元素变为元素属性 108 xs.useAttributeFor(Province.class, "name"); 109 // 去除集合属性对应元素 110 xs.addImplicitCollection(Province.class, "citys"); 111 //让类成员不生成对应的xml元素 112 xs.omitField(City.class, "explain"); 113 String s = xs.toXML(getProvince()); 114 System.out.println(s); 115 } 116 117 }
----其它涉及的类-----
Provice.class
1 import java.util.List; 2 3 public class Province { 4 private String name; 5 private List<City> citys; 6 public Province() { 7 super(); 8 // TODO Auto-generated constructor stub 9 } 10 public Province(String name, List<City> citys) { 11 super(); 12 this.name = name; 13 this.citys = citys; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public List<City> getCitys() { 22 return citys; 23 } 24 public void setCitys(List<City> citys) { 25 this.citys = citys; 26 } 27 28 29 }
City.class
1 public class City { 2 private String name; 3 private String explain; 4 public City() { 5 super(); 6 // TODO Auto-generated constructor stub 7 } 8 public City(String name, String explain) { 9 super(); 10 this.name = name; 11 this.explain = explain; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public String getExplain() { 20 return explain; 21 } 22 public void setExplain(String explain) { 23 this.explain = explain; 24 } 25 26 }
标签:
原文地址:http://www.cnblogs.com/JamKong/p/4321496.html