XStream使用笔记

XStream是一个可以将JavaBean生成XML的工具,很方便的帮助我们在后台服务器将数据序列化为XML,接下来就可以将XML数据响应给前台进行数据交互

XStream需要的jar包
* 核心JAR包:xstream.jar
* 必须依赖包:xpp3_min.jar(XML Pull Parser,一款速度很快的XML解析器)

使用起来非常简单,先把XStream对象给new出来:

XStream xstream = new XStream();

接着就可以使用XStream的对象中的方法,最常用最简单就是toXML()方法,直接就可以生成xml格式的字符串

示例代码

既然XStream可以将JavaBean转换为xml,那我们就先处理JavaBean,我想处理一个省市联动的例子(就是一个省显示其对应的市) :

city.java:

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;
    }
    
    public City() {
        super();
    }

    public City(String name) {
        super();
        this.name = name;
    }

    public City(String name, String description) {
        super();
        this.name = name;
        this.description = description;
    }
    
    
}

给出市类的构造函数,方便给值

Province.java:

import java.util.ArrayList;
import java.util.List;

public class Province {
    private String name;
    
    private List<City> citys=new ArrayList<City>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<City> getCitys() {
        return citys;
    }

    public void setCitys(List<City> citys) {
        this.citys = citys;
    }
    
    public void addCitys(City city){
        citys.add(city);
    }
}

添加一个List集合存放该省的市,addCitys()方法就是给省添加市的方法

JavaBean处理好之后,我们就可以添加省市:

public List<Province> getProvinceList(){
        Province province1=new Province();
        province1.setName("北京");
        province1.addCitys(new City("朝阳区","chaoyang"));
        province1.addCitys(new City("东城区","dongcheng"));
        Province province2=new Province();
        province2.setName("河南");
        province2.addCitys(new City("郑州","zhengzhou"));
        province2.addCitys(new City("洛阳","luoyang"));
        List<Province> list=new ArrayList<Province>();
        list.add(province1);
        list.add(province2);
        return list;
    }

接下来就可以使用了:

/**
     * 简单使用XStream
     */
    @Test
    public void fun1(){
        List<Province> list=getProvinceList();
        XStream xStream=new XStream();
        String xml = xStream.toXML(list);
        System.out.println(xml);
    }

显示的数据格式:

<list>
  <cn.lynu.model.Province>
    <name>北京</name>
    <citys>
      <cn.lynu.model.City>
        <name>朝阳区</name>
        <description>chaoyang</description>
      </cn.lynu.model.City>
      <cn.lynu.model.City>
        <name>东城区</name>
        <description>dongcheng</description>
      </cn.lynu.model.City>
    </citys>
  </cn.lynu.model.Province>
  <cn.lynu.model.Province>
    <name>河南</name>
    <citys>
      <cn.lynu.model.City>
        <name>郑州</name>
        <description>zhengzhou</description>
      </cn.lynu.model.City>
      <cn.lynu.model.City>
        <name>洛阳</name>
        <description>luoyang</description>
      </cn.lynu.model.City>
    </citys>
  </cn.lynu.model.Province>
</list>

根标签是list,是因为我们就是将省放在list中的

显示为list,并且显示的是完整的类名称,我们给它取个别名(使用alias()方法):

    /**
     * 取别名
     */
    @Test
    public void fun2(){
        List<Province> list=getProvinceList();
        XStream xStream=new XStream();
        xStream.alias("china", List.class);
        xStream.alias("province", Province.class);
        xStream.alias("city", City.class);
        System.out.println(xStream.toXML(list));
    }

显示的数据格式:

<china>
  <province>
    <name>北京</name>
    <citys>
      <city>
        <name>朝阳区</name>
        <description>chaoyang</description>
      </city>
      <city>
        <name>东城区</name>
        <description>dongcheng</description>
      </city>
    </citys>
  </province>
  <province>
    <name>河南</name>
    <citys>
      <city>
        <name>郑州</name>
        <description>zhengzhou</description>
      </city>
      <city>
        <name>洛阳</name>
        <description>luoyang</description>
      </city>
    </citys>
  </province>
</china>

嗯,格式格式好看多了

可以看到province和city都有name的子标签,我们可以把name变为它们的属性值(使用useAttributeFor()方法):

/**
     * 将类的属相变成标签的属性
     */
    @Test
    public void fun3(){
        List<Province> list=getProvinceList();
        XStream xStream=new XStream();
        xStream.alias("china", List.class);
        xStream.alias("province", Province.class);
        xStream.alias("city", City.class);
        
        xStream.useAttributeFor(Province.class, "name");
        xStream.useAttributeFor(City.class, "name");
        
        System.out.println(xStream.toXML(list));
    }

显示的数据格式:

<china>
  <province name="北京">
    <citys>
      <city name="朝阳区">
        <description>chaoyang</description>
      </city>
      <city name="东城区">
        <description>dongcheng</description>
      </city>
    </citys>
  </province>
  <province name="河南">
    <citys>
      <city name="郑州">
        <description>zhengzhou</description>
      </city>
      <city name="洛阳">
        <description>luoyang</description>
      </city>
    </citys>
  </province>
</china>

格式又精简了不少

我们可以看到竟然还有个<citys>标签,这是因为我把city也放在一个list中给province,我们可以隐藏该标签

(使用addImplicitArray()方法):

/**
     * 隐藏某个标签
     */
    @Test
    public void fun4(){
        List<Province> list=getProvinceList();
        XStream xStream=new XStream();
        xStream.alias("china", List.class);
        xStream.alias("province", Province.class);
        xStream.alias("city", City.class);
        xStream.useAttributeFor(Province.class, "name");
        xStream.useAttributeFor(City.class, "name");
        
        xStream.addImplicitArray(Province.class, "citys");
        
        System.out.println(xStream.toXML(list));
    }

显示的数据格式为:

<china>
  <province name="北京">
    <city name="朝阳区">
      <description>chaoyang</description>
    </city>
    <city name="东城区">
      <description>dongcheng</description>
    </city>
  </province>
  <province name="河南">
    <city name="郑州">
      <description>zhengzhou</description>
    </city>
    <city name="洛阳">
      <description>luoyang</description>
    </city>
  </province>
</china>

我们可以看到city标签中还有一个<description>子标签,我们也可以删除这个标签(如果不需要使用omitField()方法):

/**
     * 删除某个标签
     */
    @Test
    public void fun5(){
        List<Province> list=getProvinceList();
        XStream xStream=new XStream();
        xStream.alias("china", List.class);
        xStream.alias("province", Province.class);
        xStream.alias("city", City.class);
        xStream.useAttributeFor(Province.class, "name");
        xStream.useAttributeFor(City.class, "name");
        xStream.addImplicitArray(Province.class, "citys");
        
        xStream.omitField(City.class, "description");
        
        System.out.println(xStream.toXML(list));
    }

显示的数据格式:

<china>
  <province name="北京">
    <city name="朝阳区"/>
    <city name="东城区"/>
  </province>
  <province name="河南">
    <city name="郑州"/>
    <city name="洛阳"/>
  </province>
</china>

最后我们将xml数据的格式精简到这么少,可读性大大提高

 

posted @ 2017-07-02 23:11  OverZeal  阅读(457)  评论(0编辑  收藏  举报