• XStream详解


    XStream的作用

    XStream可以把JavaBean对象转换成XML!

    通常服务器向客户端响应的数据都是来自数据库的一组对象,而我们不能直接把对象响应给客户端,所以我们需要把对象转换成XML再响应给客户端,这时就需要使用XStream组合了

    XStream相关JAR

    我们可以到http://xstream.codehaus.org/地址去下载XStream安装包!

    XStream的必导JAR包:

    核心JAR包:xstream-1.4.7.jar;

    必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器)

    XStream举例

    首先创建两个JavaBean对象

    City类----------------------------
    public class City {
    
        private String name;
        private String description;
        
        public City(String name,String description) {
            this.name=name;
            this.description=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 + "]";
        }
    
    
    }
    Province类--------------------------
    public class Province {
    
        private String name;
        private List<City> cities = new ArrayList<City>();
        
        public Province(String name) {
            this.name=name;
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public List<City> getCities() {
            return cities;
        }
        
        public void addCity(City name) {
            cities.add(name);
        }
        @Override
        public String toString() {
            return "Province [name=" + name + ", cities=" + cities + "]";
        }
    
    
    }

    接下来,我们需要写一个main(),创建一个List,List中存放两个Province对象!最终我们把List转换成XML

    下面是使用XStream转换list为XML的代码

            Province p1 = new Province("辽宁省");
            p1.addCity(new City("沈阳", "shenyang"));
            p1.addCity(new City("大连", "dalian"));
            
            Province p2 = new Province("吉林省");
            p2.addCity(new City("长春", "changchen"));
            p2.addCity(new City("白城", "baicheng"));
            
            List<Province> list = new ArrayList<Province>();
    
            list.add(p1);
            list.add(p2);
    //--------------------------------------------------------

           XStream xstream = new XStream();

            String s = xstream.toXML(list);
            System.out.println(s);

    下面是运行结果

    <list>
      <cn.itcast.xstream.demo1.Province>
        <name>辽宁省</name>
        <cities>
          <cn.itcast.xstream.demo1.City>
            <name>沈阳</name>
            <description>shenyang</description>
          </cn.itcast.xstream.demo1.City>
          <cn.itcast.xstream.demo1.City>
            <name>大连</name>
            <description>dalian</description>
          </cn.itcast.xstream.demo1.City>
        </cities>
      </cn.itcast.xstream.demo1.Province>
      <cn.itcast.xstream.demo1.Province>
        <name>吉林省</name>
        <cities>
          <cn.itcast.xstream.demo1.City>
            <name>长春</name>
            <description>changchen</description>
          </cn.itcast.xstream.demo1.City>
          <cn.itcast.xstream.demo1.City>
            <name>白城</name>
            <description>baicheng</description>
          </cn.itcast.xstream.demo1.City>
        </cities>
      </cn.itcast.xstream.demo1.Province>
    </list>

    使用alias,在生成的XML中,与类名对应的元素名称包含了包名部分,这很不好看!若想自定义生成的元素名称,需要使用XStream为类名提供别名:

    xstream.alias("province", Province.class);
            xstream.alias("china", List.class); 
            xstream.alias("city", City.class);
    ---------------------------------------------------------------------------------------
    结果为:
    <china>
      <province>
        <name>辽宁省</name>
        <cities>
          <city>
            <name>沈阳</name>
            <description>shenyang</description>
          </city>
          <city>
            <name>大连</name>
            <description>dalian</description>
          </city>
        </cities>
      </province>
      <province>
        <name>吉林省</name>
        <cities>
          <city>
            <name>长春</name>
            <description>changchen</description>
          </city>
          <city>
            <name>白城</name>
            <description>baicheng</description>
          </city>
        </cities>
      </province>
    </china>

    把子元素变为元素属性,例如我们需要把<province>的子元素<name>变成:<province name=””>样式,那么需要调用如下方法

    xstream.useAttributeFor(Province.class, "name");
    --------------------------------------------------------------
    结果为
    <china>
      <province name="辽宁省">
        <cities>
          <city>
            <name>沈阳</name>
            <description>shenyang</description>
          </city>
          <city>
            <name>大连</name>
            <description>dalian</description>
          </city>
        </cities>
      </province>
      <province name="吉林省">
        <cities>
          <city>
            <name>长春</name>
            <description>changchen</description>
          </city>
          <city>
            <name>白城</name>
            <description>baicheng</description>
          </city>
        </cities>
      </province>
    </china>
    去除集合属性对应元素

    因为Pronvice类有一个cities成员,所以生成了<cities>元素,但这个元素对XML文档而言没有什么意义,所以我们希望把它去除!

    xstream.addImplicitCollection(Province.class, "cities");
    --------------------------------------------------------------------------
    结果为
    <china>
      <province name="辽宁省">
        <city>
          <name>沈阳</name>
          <description>shenyang</description>
        </city>
        <city>
          <name>大连</name>
          <description>dalian</description>
        </city>
      </province>
      <province name="吉林省">
        <city>
          <name>长春</name>
          <description>changchen</description>
        </city>
        <city>
          <name>白城</name>
          <description>baicheng</description>
        </city>
      </province>
    </china>
    让类的成员不生成对应XML元素

    每个类,每个成员都有对应的元素(或属性)存在,但有时我们并不希望某些类的成员在对应的XML文档中出现,例如我们不希望City类的description成员出现在XML文档中,可以使用下面方法

    xstream.omitField(City.class, "description"); //不希望description属性出现
    ----------------------------------------------------------
    结果为
    <china>
      <province name="辽宁省">
        <city>
          <name>沈阳</name>
        </city>
        <city>
          <name>大连</name>
        </city>
      </province>
      <province name="吉林省">
        <city>
          <name>长春</name>
        </city>
        <city>
          <name>白城</name>
        </city>
      </province>
    </china>
  • 相关阅读:
    缩略图(转载)
    MarteEngine tutorial:Keyboard and mouse input
    MarteEngine tutorial: Hello World
    FengGUI
    位于两个内网的结点A和B都连接到一个公网的rdv,然后A与B之间发送消息,这时消息是否还经过rdv?
    MarteEngine
    MarteEngine tutorial:Basic collision
    关于PresenceService的实现方式
    MarteEngine: Animate sprite
    MarteEngine tutorial: 设置你的环境
  • 原文地址:https://www.cnblogs.com/QianYue111/p/9818540.html
Copyright © 2020-2023  润新知