leftso 970 0 2018-01-02 20:43:47

文章位置:左搜> 编程技术> 正文

引言

   java编程中使用XStream实现java bean与XML的序列化和反序列化(一),本文主要讲解XStream的入门使用。编写一个简单的java bean通过XStream序列化为XML,将XML的字符串转反序列化成java bean。

一.环境准备

  • JDK1.6+(我这里使用的1.8)
  • eclipse(或者你自己喜欢的IDE)
  • maven 3+

二.编码使用XStream

2.1创建一个普通的maven项目

xstream java项目结构图

2.2编写两个简单的java POJO对象

PhoneNumber.java
package net.xqlee.project.demo.pojo;

public class PhoneNumber {

	public PhoneNumber(int code, String number) {
		super();
		this.code = code;
		this.number = number;
	}

	private int code;
	private String number;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

}

Person.java
package net.xqlee.project.demo.pojo;

public class Person {
	public Person() {
	}

	public Person(String firstname, String lastname) {
		super();
		this.firstname = firstname;
		this.lastname = lastname;
	}

	private String firstname;
	private String lastname;
	private PhoneNumber phone;
	private PhoneNumber fax;

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public PhoneNumber getPhone() {
		return phone;
	}

	public void setPhone(PhoneNumber phone) {
		this.phone = phone;
	}

	public PhoneNumber getFax() {
		return fax;
	}

	public void setFax(PhoneNumber fax) {
		this.fax = fax;
	}

}

2.3编写一个测试类来通过XStream序列化java bean和XML反序列化

XTest.java
package net.xqlee.project.demo.xstream;

import com.thoughtworks.xstream.XStream;

import net.xqlee.project.demo.pojo.Person;
import net.xqlee.project.demo.pojo.PhoneNumber;

public class XTest {

	public static void main(String[] args) {
		try {
			// ============初始化==================
			// 这种初始化方法需要依赖XPP3,XPP3是一个转换XML的实现
			XStream xstream = new XStream();

			// 不需要XPP3依赖,使用DomDriver替换XPP3
			// XStream xstream = new XStream(new DomDriver());

			// 不需要XPP3依赖, JDK1.6以上
			// XStream xstream = new XStream(new StaxDriver());

			// =============对象序列化成XML============
			// 创建测试数据
			Person person = new Person("左", "搜");
			person.setFax(new PhoneNumber(023, "45215778"));
			person.setPhone(new PhoneNumber(86, "1355469901"));

			String xml = xstream.toXML(person);
			System.out.println(xml);
			System.out.println("--------------华丽的分割线--------------");

			// 设置别名
			xstream.alias("person", Person.class);
			xstream.alias("phonenumber", PhoneNumber.class);

			xml = xstream.toXML(person);
			System.out.println(xml);

			// =============XML解码成java对象============
			Person person2 = (Person) xstream.fromXML(xml);

			System.out.println("name:" + person2.getFirstname() + "," + person2.getLastname());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

三.演示

该例子是一个简单的java application,直接eclipse右键运行java application即可,观察控制台输出:
xstream运行结果
可以看到已经通过xstream将java bean转换为XML,并且通过别名将默认的XML标签改成简单的类名。

一个简单的XStream使用教程描述玩啦。