XStream-->别名;元素转属性;去除集合属性(剥皮);忽略不需要元素

Posted vmkash

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XStream-->别名;元素转属性;去除集合属性(剥皮);忽略不需要元素相关的知识,希望对你有一定的参考价值。

--->清单:

City.java

Province.java

TestXStream.java

4个XStream方法的效果图


 

javabean-->City.java

 

 1 package xstream;
 2 
 3 public class City {
 4     private String name;
 5     private String description;
 6     
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public String getDescription() {
14         return description;
15     }
16     public void setDescription(String description) {
17         this.description = description;
18     }
19     public City(String name, String description) {
20         this.name = name;
21         this.description = description;
22     }
23 }

javabean-->Province.java

 

 1 package xstream;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class Province {
 7     private String name;
 8     private List<City> cities = new ArrayList<City>();
 9     
10     public void addCity(City city){
11         cities.add(city);
12     }
13     
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public List<City> getCities() {
21         return cities;
22     }
23     public void setCities(List<City> cities) {
24         this.cities = cities;
25     }
26     public Province(String name, List<City> cities) {
27         super();
28         this.name = name;
29         this.cities = cities;
30     }
31 
32     public Province() {}
33 
34     public Province(String name) {
35         this.name = name;
36     }
37 }

测试类-->TestXStream.java

 1 package xstream;
 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 TestXStream {
11     public List<Province> getProvinceList(){
12         List<Province> provinceList = new ArrayList<Province>();
13         
14         Province p1 = new Province("北京");
15         p1.addCity(new City("东城区","东城区"));
16         p1.addCity(new City("西城区","西城区"));
17         
18         Province p2 = new Province("上海");
19         p2.addCity(new City("徐汇区","徐汇区"));
20         p2.addCity(new City("浦东新区","浦东新区"));
21         
22         provinceList.add(p1);
23         provinceList.add(p2);
24         
25         return provinceList;
26     }
27     //--->原生版
28     @Test
29     public void fun(){
30         List<Province> proList = getProvinceList();
31         XStream xStream = new XStream();
32         String xml = xStream.toXML(proList);
33         System.out.println(xml);
34     }
35 
36     //--->修改
37     @Test
38     public void fun1(){
39         List<Province> proList = getProvinceList();
40         XStream xStream = new XStream();
41         
42         //别名
43         xStream.alias("china", java.util.List.class);
44         xStream.alias("province", Province.class);
45         xStream.alias("city", City.class);
46         
47         //把子元素变成父元素的属性
48         xStream.useAttributeFor(Province.class, "name");
49         
50         //去掉容器,"剥皮"
51         xStream.addImplicitCollection(Province.class, "cities");
52         
53         //忽略不需要的属性-xml的元素
54         xStream.omitField(City.class, "description");
55         
56         String xml = xStream.toXML(proList);
57         System.out.println(xml);
58     }
59     
60 }

-->4个XStream方法的效果图


 

--->别名


 

--->子元素转换成属性


--->去掉集合元素("剥皮")


 

--->忽略不需要的元素



 

以上是关于XStream-->别名;元素转属性;去除集合属性(剥皮);忽略不需要元素的主要内容,如果未能解决你的问题,请参考以下文章

详解XStream别名

xstream 复杂结构的xml转javabean

xStream完美转换XMLJSON(转)

XStream 和下划线

Java 对象转XML xStream 附下载方式

使用XStream是实现XML与Java对象的转换--别名