XStream简单对象使用

 

XStream的优点很多,但是也有一些小bug,比如在定义别名中的下划线“_”转换为xml后会变成“__”这个符号,很变态。因此,尽量避免在别名中实用任何符号,却是需要下划线的时候,可以考虑实用连接符“-”,这个没有问题。
 
另外,我们的Java Bean中,常常有一些常量,在转换过程,XStream也会将这些常量转换过去,形成常量的xml节点,这显然不是想要的结果,对于常量字段,就不做转换了。

Category.java

 

public class Category {

	private int id;
	private String name;
	private String remark;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

}

 Content.java

 

public class Content {
	private int id;
	private String title;
	private String content;
	private String createTime;
	private String updateTime;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}
}

 Java对象转换成XML:

 

package com;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import com.model.Content;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Object2XML {
	static XStream xs = new XStream(new DomDriver());
	static String xmlContent;

	public static void main(String args[]) {
		Content content = new Content();
		content.setId(001);
		content.setTitle("XStreamTest");
		content.setContent("ooxx");
		content.setCreateTime("yesterday");
		content.setUpdateTime("now");

		try {
			String location = "d:/temp/object2xml.xml";
			File file = new File(location);
			if(!file.exists()||file.isDirectory()){
				String[] arry=location.split("/");
				File file1=new File(arry[0]+"/"+arry[1]); //创建文件夹后如果后面对文件操作的话会自动创建文件
				file1.mkdir();
			}
			FileOutputStream fos = new FileOutputStream(file); //提示:字节流
			OutputStreamWriter osw = new OutputStreamWriter(fos, Charset.forName("gbk")); //作用:使字节流转换成字符流  osw是字符流通向字节流的桥梁,使用Charset指定字符编码
			//ows调用write()方法会调用转换器
			BufferedWriter bw = new BufferedWriter(osw); //提示:为了获得最高效率,把osw包装到bw中,这样做可以避免频繁调用转换器
			bw.write("<?xml version=\"1.0\" encoding=\"gbk\" ?>\n"); //提示:在生成的xml前面加上基本信息
			xs.alias("content", Content.class);  //提示:给类取别名
			xs.aliasField("内容", Content.class, "content");  //提示:给属性取别名
			xs.toXML(content, bw);
			bw.close();  //作用:关闭流
			osw.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 结果:

 

<?xml version="1.0" encoding="gbk" ?>
<content>
  <id>1</id>
  <title>XStreamTest</title>
  <内容>ooxx</内容>
  <createTime>yesterday</createTime>
  <updateTime>now</updateTime>
</content>

 XML转换成Java对象:

 

package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import com.model.Content;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XML2Object {

	static XStream xs = new XStream(new DomDriver());

	public static void main(String args[]) {
		File file = new File("d:/temp/object2xml.xml"); // 名字区分大小写!
		Content content = new Content();
		try {
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isw = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isw);
			xs.alias("content", Content.class);// 生成与解析时代别名要对应!!!!
			xs.aliasField("内容", Content.class, "content");
			xs.fromXML(br, content);
			br.close();
			isw.close();
			fis.close();
			System.out.println("id=" + content.getId());
			System.out.println("title=" + content.getTitle());
			System.out.println("content=" + content.getContent());
			System.out.println("createTime=" + content.getCreateTime());
			System.out.println("updateTime=" + content.getUpdateTime());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 结果:

 

id=1

title=XStreamTest

content=ooxx

createTime=yesterday

updateTime=now


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值