XStream小使用

系统 1988 0
XStream

目录
1 XSTREAM框架 1
1.1 XSTREAM的作用 1
1.2 XSTREAM的说明 1
2 XSTREAM的使用 1
2.1 JAVABEAN转换为XML 1
2.2 XML转换为JAVABEAN 1
2.3 JAVABEAN转化为JSON 1
2.4 JSON转化为JAVABEAN 1

1 XStream框架
1.1 XStream的作用
XStream是一个开源项目,它可以实现javaBean和xml,json之间进行轻松的转换。XStream支持将javaBean转换为xml,同时也支持将xml转换为javaBean,当然也可以将javaBean转换问json,也可以将json转换问javaBean。

1.2 XStream的说明
官网:http://xstream.codehaus.org/index.html
下载地址:http://xstream.codehaus.org/download.html
官方使用示例:http://xstream.codehaus.org/tutorial.html
打开下载地址,XStream提供了src(源码)和bin(二进制)包下载,如果想了解学习源代码可以下载src,如果只想使用就下载bin。下面简绍XStream的使用。

2 XStream的使用
下载XSteam的bin包,在java项目中加入下面包。



创建java项目,创建转换模型User和Bbirthday类,创建一个TestUser来作为测试转换类。
XStream的toXml()方法实现了javaBean到xml,json的转化,fromXml()方法实现了xml,json到javaBean的转换。
User类

    package com.model;

import com.thoughtworks.xstream.annotations.XStreamAlias;

/**
 * @author xmong
 */
//annotation注解别名,即xml中的user节点名称
@XStreamAlias("user")
public class User {
	
	//姓名
	public String name;
	//生日
	public Birthday birthday;
	
	public User(){
		super();
	}
	
	public User(String name, Birthday birthday) {
		this.name = name;
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Birthday getBirthday() {
		return birthday;
	}
	public void setBirthday(Birthday birthday) {
		this.birthday = birthday;
	}
		
	@Override
	public String toString() {
		return "{ name:"+this.name+"; birthday:"+this.birthday+"}";
	}

}

  

Birthday类

    package com.model;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

//annotation注解别名,即xml中的user节点名称
@XStreamAlias("birthday")
public class Birthday {
	
	//annotation注解属性,即xml中节点属性
	@XStreamAsAttribute
	int year;
	@XStreamAsAttribute
	int month;
	int day;
	
	public Birthday(){
		super();
	}
	
	public Birthday(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	
	@Override
	public String toString() {
		return "{year:"+this.year+"; month"+this.month+"; day"+this.day+"}";
	}

}
  


2.1 JavaBean转化为XML

TestUser类

    package com.model;

import com.thoughtworks.xstream.XStream;

/**
 * @author xmong
 */
public class TestUser {
	
	/**
	 * javaBean转换为xml
	 */
	public void test1(){
		
		//创建user对象
		User user = new User();
		user.setName("xmong");
		user.setBirthday(new Birthday(1990, 10, 20));
		
		//创建xstream
		XStream xstream = new XStream();
		//XStream xstream = new XStream(new DomDriver());
		//XStream xstream = new XStream(new StaxDriver());

		//加载annotation注解
		xstream.processAnnotations(User.class);
		
		System.out.println(xstream.toXML(user));
		
	}

       public static void main(String[] args) {
		TestUser tu = new TestUser();
		tu.test1();
	}
	
}
  


输出结果:
    <user>
  <name>xmong</name>
  <birthday year="1990" month="10">
    <day>20</day>
  </birthday>
</user>
  


如果使用
    XStream xstream = new XStream(new DomDriver())
  

创建XSteam对象那么输出的结果同上。
如果使用
    XStream xstream = new XStream(new StaxDriver())
  

来创建XStream对象,输出的结果:
    <?xml version="1.0" ?><user><name>xmong</name><birthday year="1990" month="10"><day>20</day></birthday></user>
  



2.2 XML转化为JavaBean

在TestUser的main方法调用test2方法
    /**
	 * XML转换为JavaBean
	 */
	public void test2(){
		
		//创建user对象
		User user = new User();
		user.setName("xmong");
		user.setBirthday(new Birthday(1990, 10, 20));
		
		//创建xstream
		XStream xstream = new XStream();
		//加载annotation注解
		xstream.processAnnotations(User.class);
		
		String xml = xstream.toXML(user);
		
		System.out.println(xml);
		
		System.out.println("===================");
		
		System.out.println((User)xstream.fromXML(xml));
		
	}

	public static void main(String[] args) {
		TestUser tu = new TestUser();
		tu.test2();
	}


  


输出结果
    <user>
  <name>xmong</name>
  <birthday year="1990" month="10">
    <day>20</day>
  </birthday>
</user>
===================
{ name:xmong; birthday:{year:1990; month10; day20}}
  


2.3 JavaBean转化为Json

    /**
	 * javaBean转换为json
	 */
	public void test3(){
		
		//创建user对象
		User user = new User();
		user.setName("xmong");
		user.setBirthday(new Birthday(1990, 10, 20));
		
		//创建xstream,传入转换驱动类JettisonMappedXmlDriver
		XStream xstream = new XStream( new JettisonMappedXmlDriver());
		//设置user类的json别名
		xstream.alias("user", User.class);
		
		System.out.println(xstream.toXML(user));
		
	}


	public static void main(String[] args) {
		TestUser tu = new TestUser();
		tu.test3();
	}
  



输出结果
    {"user":{"name":"xmong","birthday":{"year":1990,"month":10,"day":20}}}
  


    /**
	 * javaBean转换为json
	 */
	public void test4(){
		
		//创建user对象
		User user = new User();
		user.setName("xmong");
		user.setBirthday(new Birthday(1990, 10, 20));
		
		//创建xstream,传入转换驱动类JsonHierarchicalStreamDriver
		XStream xstream = new XStream( new JsonHierarchicalStreamDriver());
		/*//去除根节点
		XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
		    public HierarchicalStreamWriter createWriter(Writer writer) {
		        return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
		    }
		});
		*/

		//设置user类的json别名
		xstream.alias("user", User.class);
		
		System.out.println(xstream.toXML(user));
		
	}

	public static void main(String[] args) {
		TestUser tu = new TestUser();
		tu.test4();
	}
  


输出结果

    {"user": {
  "name": "xmong",
  "birthday": {
    "year": 1990,
    "month": 10,
    "day": 20
  }
}}
  

如果使用
    XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
		    public HierarchicalStreamWriter createWriter(Writer writer) {
		        return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
		    }
		});
  

创建XStream对象,那么java对象转换成的json会去除根节点。
输出结果
    {
  "name": "xmong",
  "birthday": {
    "year": 1990,
    "month": 10,
    "day": 20
  }
}
  



2.4 Json转化为JavaBean

    /**
	 * json转换为javaBean
	 */
	public void test5(){
		
		//创建user对象
		User user = new User();
		user.setName("xmong");
		user.setBirthday(new Birthday(1990, 10, 20));
		
		//创建xstream,传入转换驱动类JettisonMappedXmlDriver
		XStream xstream = new XStream( new JettisonMappedXmlDriver());
		//设置user类的json别名
		xstream.alias("user", User.class);
		
		String json = xstream.toXML(user);
		
		System.out.println(json);
		System.out.println("================");
		System.out.println(xstream.fromXML(json));
		
	}
	
	public static void main(String[] args) {
		TestUser tu = new TestUser();
		tu.test5();
	}
  


输出结果
    {"user":{"name":"xmong","birthday":{"year":1990,"month":10,"day":20}}}
================
{ name:xmong; birthday:{year:1990; month10; day20}}
  





XStream小使用


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论