XStream之初识

官网: http://xstream.codehaus.org/download.html
相关文件可在官网中去下载
来源于:
你的第一杯Web 2.0 —— 快速浏览jQuery、Spring MVC和XStream/Jettison
http://www.infoq.com/cn/articles/First-Cup-Web-2.0-Joel-Confino
参考资料
1 XStream两分钟教程
http://www.duduwolf.com/wiki/2007/390.html
2 XStream使用方法总结附实例代码
http://www.jb51.net/article/14542.htm
3 使用 XStream 把 Java 对象序列化为 XML
http://www.ibm.com/developerworks/cn/xml/x-xstream/

XStram说明
XStream是一个Java对象和XML相互转换的工具,提供了所有的基础类型、数组、集合等类型直接转换的支持.XStream对象相当Java对象和XML之间的转换器,转换过程是双向.使用XStream 不用任何映射就能实现多数 Java 对象的序列化。在生成的 XML 中对象名变成了元素名,类中的字符串组成了 XML 中的元素内容。使用 XStream 序列化的类不需要实现 Serializable 接口。XStream 是一种序列化工具而不是数据绑定工具,就是说不能从 XML 或者 XML Schema Definition (XSD) 文件生成类。
和其他序列化工具相比,XStream 有三个突出的特点:
XStream 不关心序列化/逆序列化的类的字段的可见性。
序列化/逆序列化类的字段不需要 getter 和 setter 方法。
序列化/逆序列化的类不需要有默认构造函数。
不需要修改类,使用 XStream 就能直接序列化/逆序列化任何第三方类

第一个示例
实体类
public class Employee {
	private String name;
	private String designation;
	private String department;

	public String getName() {
		return name;
	}

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

	public String getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "Name : " + this.name + "\nDesignation : " + this.designation
				+ "\nDepartment : " + this.department;
	}
}


写入类
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.thoughtworks.xstream.*;

public class Writer {

	public static void main(String[] args) {
		Employee e = new Employee();	
		e.setName("Jack");
		e.setDesignation("Manager");
		e.setDepartment("Finance");		
		XStream xs = new XStream();
		try {
			FileOutputStream fs = new FileOutputStream("c:/temp/employeedata.txt");
			xs.toXML(e, fs);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}
}


读取类
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Reader {

	public static void main(String[] args) {
		XStream xs = new XStream(new DomDriver());
		Employee e = new Employee();

		try {
			FileInputStream fis = new FileInputStream("c:/temp/employeedata.txt");
			xs.fromXML(fis, e);
			System.out.println(e.toString());
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
	}
}


第二个示例
Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
 <datasource-name>IRIS</datasource-name>
 <ipaddress>9.124.74.85</ipaddress>
 <logfilename>DailyLogApplication.log</logfilename>
 <appender>console</appender>
</config>

import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class ConfigReader {

	String datasourcename = null;
	String ipaddress = null;
	String logfilename = null;
	String appender = null;

	@Override
	public String toString() {		
		return "Datasource Name : " + datasourcename + " \nIP Address : "
				+ ipaddress + " \nLogfilename : " + logfilename
				+ " \nAppender : " + appender;
	}

	/**
	 * @param args
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException {
		XStream xs = new XStream(new DomDriver());		
		FileInputStream fis = new FileInputStream("c:/temp/Config.xml");
		xs.aliasField("datasource-name", ConfigReader.class, "datasourcename");
		xs.alias("config", ConfigReader.class);		
		ConfigReader r = (ConfigReader) xs.fromXML(fis);
		System.out.println(r.toString());
	}
}


第三个示例
Person.java
public class Person {
    private String firstName;
    private String lastName;
    private PhoneNumber phone;
    private PhoneNumber fax;   

    public Person(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
    }
	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;
	}  
	public String toString(){
		   return "firstName: " + firstName + ",lastName: " + lastName+",phone: [" + phone+"],fax: [" + fax+"]";
	}
}


PhoneNumber.java
public class PhoneNumber {
    private int code;
    private String number;
    
    public PhoneNumber(int code, String number) {
        this.code = code;
        this.number = 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;
	}
   public String toString(){
	   return "code: " + code + ",number: " + number;
   }
}


测试类
import com.thoughtworks.xstream.XStream;

public class Test {

    public static void main(String[] args) {   
       // XStream默认的构造函数是调用XPP3来解析XML的,
       // XPP3是一个pull-parser实现,据说这是目前最快的解析XML的类
       // 我们也可以不用这个,还用JAXP去解析
      // XStream xstream = new XStream(new DomDriver()); 
       XStream xstream = new XStream();      
       // 为类设置别名
       // 如果不设置,生成的XML会是类的全称,含包名
       xstream.alias("person", Person.class);
       xstream.alias("phonenumber", PhoneNumber.class);     
       Person joe = new Person("Joe", "Walnes");
       joe.setPhone(new PhoneNumber(123, "1234-456"));
       joe.setFax(new PhoneNumber(123, "9999-999"));    
       // 解析Object成XML
       String xml = xstream.toXML(joe);       
       System.out.println("xml: "+xml);    
       // 把XML解析成Object
       Person newJoe = (Person)xstream.fromXML(xml);
       System.out.println("newJoe: "+newJoe);
    }

}

jar见附件

你可能感兴趣的:(java,spring,jquery,xml,mvc)