XStream 用法详解 java 类与 XML 互换

编程技术  /  houtizong 发布于 3年前   111

最近要用到XML和VO的转换,花半天研究了下XStream记录一下结果。

首先是例子entity类 Person;

package com.xstream.entityimport com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;import com.thoughtworks.xstream.annotations.XStreamImplicit;import java.util.ArrayList;import java.util.List;/** * User: Rojer * Date: 12-3-1 * Time: 下午4:58 * To change this template use File | Settings | File Templates. *///设置Person类在xml中的别名@XStreamAlias("person")public class Person {    //将name设置为XML person 元素的 attribute    @XStreamAsAttribute()    private String name;    private int phoneNuber;    //将此字段名在XML中去掉    @XStreamImplicit()    private List<Address> addresses = new ArrayList<Address>();    public List<Address> getAddresses() {        return addresses;    }    public void setAddresses(List<Address> addresses) {        this.addresses = addresses;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getPhoneNuber() {        return phoneNuber;    }    public void setPhoneNuber(int phoneNuber) {        this.phoneNuber = phoneNuber;    }}

   Adress

package com.xstream.entity;import com.thoughtworks.xstream.annotations.XStreamAlias;/** * User: Rojer * Date: 12-3-1 * Time: 下午5:00 * To change this template use File | Settings | File Templates. */@XStreamAlias("address")public class Address {    private String street;    private int houseNo;    public String getStreet() {        return street;    }    public void setStreet(String street) {        this.street = street;    }    public int getHouseNo() {        return houseNo;    }    public void setHouseNo(int houseNo) {        this.houseNo = houseNo;    }}

 测试类

package com.xstream.test;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.io.xml.Dom4JDriver;import com.thoughtworks.xstream.io.xml.DomDriver;import com.thoughtworks.xstream.io.xml.StaxDriver;import com.xstream.entity.Address;import com.xstream.entity.Person;import org.xmlpull.mxp1.MXParser;import java.io.ObjectInputStream;import java.io.StringReader;/** * User: Rojer * Date: 12-3-1 * Time: 下午4:48 * To change this template use File | Settings | File Templates. */public class TestStream {        public static void main(String[] args) {        String res=toXML();        toEntity(res);    }        public static void toEntity(String inputXML){        XStream xs = new XStream();//        这句和@XStreamAlias("person")等效//        xs.alias("person",Person.class);//        xs.alias("address",Address.class);        xs.setMode(XStream.NO_REFERENCES);//      这句和@XStreamImplicit()等效//        xs.addImplicitCollection(Person.class,"addresses");//        这句和@XStreamAsAttribute()//        xs.useAttributeFor(Person.class, "name");        //注册使用了注解的VO        xs.processAnnotations(new Class[]{Person.class,Address.class});        Person person = (Person)xs.fromXML(inputXML);        System.out.println(person.getAddresses().get(0).getHouseNo()+person.getName());            }        public static String toXML(){        XStream xStream = new XStream();        Person person = new Person();        person.setName("rojer");        person.setPhoneNuber(999);        Address address1 = new Address();        address1.setHouseNo(888);        address1.setStreet("newyork");        Address address2 = new Address();        address2.setHouseNo(76767);        address2.setStreet("toyo");        person.getAddresses().add(address1);        person.getAddresses().add(address2);//        xStream.alias("person", Person.class);//        xStream.alias("address",Address.class);        xStream.setMode(XStream.NO_REFERENCES);//        xStream.addImplicitCollection(Person.class, "addresses");//        xStream.useAttributeFor(Person.class,"name");        //注册使用了注解的VO        xStream.processAnnotations(new Class[]{Person.class,Address.class});        String xml = xStream.toXML(person);        System.out.println(xml);        return xml;    }}

 测试结果如下

<person name="rojer">  <phoneNuber>999</phoneNuber>  <address>    <street>newyork</street>    <houseNo>888</houseNo>  </address>  <address>    <street>toyo</street>    <houseNo>76767</houseNo>  </address></person>888rojer

 如果去掉Person类的@XStreamImplicit()注解,生成的XML如下

<person name="rojer">  <phoneNuber>999</phoneNuber>  <addresses>    <address>      <street>newyork</street>      <houseNo>888</houseNo>    </address>    <address>      <street>toyo</street>      <houseNo>76767</houseNo>    </address>  </addresses></person>888rojer
 其字段addresses 会被写入到XML中。

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客