• 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方法的效果图


    --->别名


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


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


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



  • 相关阅读:
    随笔12 java反射机制
    随笔11 J2EE中常用的名词解释
    随笔⑩ java中的基本数据类型的基础知识
    随笔⑨ java中的变量 --- 类变量(静态变量),final变量,成员变量,局部变量 java中的方法 --- 类方法(静态方法),final方法,成员方法(实例方法),构造方法
    随笔⑧ java中的存根类 --- Stub
    随笔⑦ Java中的比较 ==,equals以及精度对比较的影响
    随笔⑥ 关于线程 --- 线程操作的主要方法
    Jupyter notebook and Octave kernel installation
    [C7] Andrew Ng
    [C6] Andrew Ng
  • 原文地址:https://www.cnblogs.com/vmkash/p/5524809.html
Copyright © 2020-2023  润新知