XStream是一个Java对象和XML相互转换的工具,提供了所有的基础类型、数组、集合等类型直接转换的支持.XStream对象相当Java对象和XML之间的转换器,转换过程是双向.使用XStream 不用任何映射就能实现多数 Java 对象的序列化。在生成的 XML 中对象名变成了元素名,类中的字符串组成了 XML 中的元素内容。使用 XStream 序列化的类不需要实现 Serializable 接口。XStream 是一种序列化工具而不是数据绑定工具,就是说不能从 XML 或者 XML Schema Definition (XSD) 文件生成类。
和其他序列化工具相比,XStream 有三个突出的特点:
XStream 不关心序列化/逆序列化的类的字段的可见性。
序列化/逆序列化类的字段不需要 getter 和 setter 方法。
序列化/逆序列化的类不需要有默认构造函数。
不需要修改类,使用 XStream 就能直接序列化/逆序列化任何第三方类
Address.java
public class Address { private int id; private String add; private String zipcode; public Address(int id, String add, String zipcode) { this.id = id; this.add = add; this.zipcode = zipcode; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } }
Person.java
import java.util.List; public class Person { private int id; private int sex; private int age; private String name; private List<Address> addList; public Person(int id, int sex, int age, String name, List<Address> addList) { this.id = id; this.sex = sex; this.age = age; this.name = name; this.addList = addList; } 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 int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Address> getAddList() { return addList; } public void setAddList(List<Address> addList) { this.addList = addList; } }
集合对象转换成XML:
import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class List2XML { public static void main(String args[]) { XStream xs = new XStream(new DomDriver()); Address address1 = new Address(001, "经三路", "450000"); Address address2 = new Address(002, "朝阳区", "10000"); List<Address> list = new ArrayList<Address>(); list.add(address1); list.add(address2); Person person = new Person(001, 1, 26, "spark", list); xs.alias("person", Person.class); // 类别名 xs.alias("address", Address.class); xs.aliasField("addressList", Person.class, "addList"); // 类成员别名 xs.aliasField("addressId", Address.class, "id"); xs.useAttributeFor(Address.class, "zipcode"); // 设置某个节点为父节点上的一个属性 // xs.aliasField("ZipCode", Address.class, "zipcode"); 与下面效果一样 xs.aliasAttribute(Address.class, "zipcode", "ZipCode"); File file=new File("d:/temp/List2XML.xml"); if(!file.exists()||!file.isDirectory()){ String location=file.getAbsolutePath(); String[] arry=location.split("\\\\"); File file1=new File(arry[0]+"/"+arry[1]); file1.mkdir(); } try { FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw=new OutputStreamWriter(fos,Charset.forName("utf-8")); BufferedWriter bw=new BufferedWriter(osw); xs.toXML(person,bw); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
结果:
<person> <id>1</id> <sex>1</sex> <age>26</age> <name>spark</name> <addressList> <address ZipCode="450000"> <addressId>1</addressId> <add>经三路</add> </address> <address ZipCode="10000"> <addressId>2</addressId> <add>朝阳区</add> </address> </addressList> </person>
XML转换成集合对象:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Iterator; import java.util.List; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class XML2List { public static void main(String args[]){ XStream xs=new XStream(new DomDriver()); File file=new File("d:/temp/List2XML.xml"); try { FileInputStream fis=new FileInputStream(file); xs.alias("person", Person.class); xs.alias("address", Address.class); xs.aliasAttribute(Person.class,"addList", "addressList"); xs.aliasAttribute(Address.class, "id", "addressId"); xs.aliasAttribute(Address.class, "zipcode", "ZipCode"); //前后顺序无关 xs.useAttributeFor(Address.class, "zipcode"); Person person=(Person) xs.fromXML(fis); System.out.println("name="+person.getName()); List list=person.getAddList(); Iterator iter=list.iterator(); Address add; System.out.println("地址信息:"); while(iter.hasNext()){ add=(Address) iter.next(); System.out.println(add.getAdd()); System.out.println(add.getZipcode()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }
结果:
name=spark
地址信息:
经三路
450000
朝阳区
10000