javabean转换成xml(XStream)小工具
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 } 272 }
province:
public class Province { private String name;// 省名 private List<City> cities = new ArrayList<City>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public List<City> getCities() { return cities; } public void setCities(List<City> cities) { this.cities = cities; } public void addCity(City city) { cities.add(city); }
city:
public class City { private String name;//市名 private String description;//描述 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "City [name=" + name + ", description=" + description + "]"; } public City() { super(); // TODO Auto-generated constructor stub } public City(String name, String description) { super(); this.name = name; this.description = description; }