XStream基本用法

Xstream是一种OXMapping 技术,是用来处理XML文件序列化的框架,在将javaBean序列化,或将XML文件反序列化的时候,不需要其它辅助类和映射文件,使得XML序列化不再繁索。

 

一、所需jar包

    下载xstream-x.x.x.jar,放到项目中

二、无anotation实例

1、先创建一个Person.java

package com.xu.xstream;

import java.util.List;

public class Person {

	private int id;

	private String name;

	private String sex;

	private List<Address> address;

	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 getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public List<Address> getAddress() {
		return address;
	}

	public void setAddress(List<Address> address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ",  sex=" + sex+ ", address=" + address + "]";
	}

}

 

2、创建PersonXStream.java操作

 

package com.xu.xstream;

import java.util.ArrayList;
import java.util.List;

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

/**
 * PersonXStream.java
 * @author XuRuiLu
 *
 * 2015-3-2
 */
public class PersonXStream {

	/**
	 * java对象转换成XML
	 * @param person
	 * @return
	 */
	public static String javaObjectToXml(Person  person){
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("Person", Person.class);	//使用别名
		xstream.alias("Address", Address.class);	//使用别名
//		xstream.useAttributeFor(Person.class, "name");	//将本类中的字段来作为xml的属性
		xstream.addImplicitCollection(Person.class, "address");	//隐身集合
		return xstream.toXML(person);
	}
	
	/**
	 * Xml转换成java对象
	 * @param xml
	 * @return
	 */
	public static Person xmlToJavaObject(String xml){
		Person person = null;
		XStream xstream = new XStream(new DomDriver());
		
		//注意这里前面是用别名,这里也要用别名,不然会报错找不到节点
		xstream.alias("Person", Person.class);
		xstream.alias("Address", Address.class);
		xstream.addImplicitCollection(Person.class, "address");	//隐身集合
		person = (Person)xstream.fromXML(xml);
		return person;
	}
	
	/**
	 * 测试javaObjectToXml方法
	 */
	public static void testJavaObjectToXml(){
		List<Address> addressList = new ArrayList<Address>();
		Address address = new Address("郑州市经三路", "123456"); 
		Address address2 = new Address("徐州市经三路", "1234"); 
		addressList.add(address);
		addressList.add(address2);
		Person person = new Person();
		person.setId(1);
		person.setName("zhangsan");
		person.setSex("男");
		person.setAddress(addressList);
		String str = javaObjectToXml(person);
		System.out.println(str);
	}
	
	/**
	 * 测试XmlToJavaObject方法
	 */
	public static void testXmlToJavaObject(){
		String xml = "<Person><id>1</id><name>zhangsan</name><sex>男</sex><Address><add>郑州市经三路</add>" +
				"<zipcode>123456</zipcode></Address><Address><add>徐州市经三路</add><zipcode>1234</zipcode></Address></Person>";
		Person person = xmlToJavaObject(xml);
		System.out.println(person.toString());
	}
	
	public static void main(String[] args) {
//		testJavaObjectToXml();	//测试java对象转成Xml
		
		testXmlToJavaObject();	//测试XmlToJavaObject方法
	}
}

 三、anotation操作

1、先创建User.java,Address.java类

    User.java

package com.xu.xstream;

import java.util.List;

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

@XStreamAlias("User") //别名
public class User {

	private int id;

	//@XStreamOmitField  
	private String name;

	private String password;

	@XStreamImplicit	//隐式集合
	private List<Address> addressList;

	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 getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password
				+ ", addressList=" + addressList + "]";
	}

}

   Address.java

package com.xu.xstream;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("Address")
public class Address {
	private String add;
	private String zipcode;

	public Address() {

	}

	public Address(String add, String zipcode) {
		this.add = add;
		this.zipcode = zipcode;
	}

	public String getAdd() {
		return add;
	}

	public void setAdd(String add) {
		this.add = add;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	@Override
	public String toString() {
		return "Address [add=" + add + ", zipcode=" + zipcode + "]";
	}
	
	

}

3、创建UserTest.java测试

package com.xu.xstream;

import java.util.ArrayList;
import java.util.List;

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

public class UserTest {
	
	/**
	 * 测试java对象转换XMl
	 */
	public static void testJavaObjectToXml(){
		XStream xstream = new XStream(new DomDriver());
		xstream.processAnnotations(User.class);
		User user = new User();
		user.setId(1);
		user.setName("zhangsan");
		user.setPassword("password");
		List<Address> addressList = new ArrayList<Address>();
		Address address = new Address("郑州市经三路", "123456"); 
		Address address2 = new Address("徐州市经三路", "1234"); 
		addressList.add(address);
		addressList.add(address2);
		user.setAddressList(addressList);
		System.out.println(xstream.toXML(user));
	}
	
	/**
	 * 测试XML转换成java对象
	 */
	public static void testXmlToJavaObject(){
		String xml = "<User><id>1</id><name>zhangsan</name><password>password</password>" +
				"<Address><add>郑州市经三路</add><zipcode>123456</zipcode>" +
				"</Address><Address><add>徐州市经三路</add><zipcode>1234</zipcode>" +
				"</Address></User>";
		User user = null;
		XStream xstream = new XStream(new DomDriver());
		xstream.processAnnotations(User.class);
		//注意这里前面是用别名,这里也要用别名,不然会报错找不到节点
		user = (User)xstream.fromXML(xml);
		System.out.println(user.toString());
	}

	public static void main(String[] args) {
//		testJavaObjectToXml();	//调用 测试java对象转换XMl方法
		
		testXmlToJavaObject();	//调用  测试XML转换成java对象方法
		
	}
}

 

你可能感兴趣的:(xstream,微风,XStream入门,XStream操作)