最近要用到XML和VO的转换,花半天研究了下XStream记录一下结果。
首先是例子entity类 Person;
package com.xstream.entity
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import java.util.ArrayList;
import java.util.List;
/**
* User: Rojer
* Date: 12-3-1
* Time: 下午4:58
* To change this template use File | Settings | File Templates.
*/
//设置Person类在xml中的别名
@XStreamAlias("person")
public class Person {
//将name设置为XML person 元素的 attribute
@XStreamAsAttribute()
private String name;
private int phoneNuber;
//将此字段名在XML中去掉
@XStreamImplicit()
private List<Address> addresses = new ArrayList<Address>();
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhoneNuber() {
return phoneNuber;
}
public void setPhoneNuber(int phoneNuber) {
this.phoneNuber = phoneNuber;
}
}
Adress
package com.xstream.entity;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* User: Rojer
* Date: 12-3-1
* Time: 下午5:00
* To change this template use File | Settings | File Templates.
*/
@XStreamAlias("address")
public class Address {
private String street;
private int houseNo;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public int getHouseNo() {
return houseNo;
}
public void setHouseNo(int houseNo) {
this.houseNo = houseNo;
}
}
测试类
package com.xstream.test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.Dom4JDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.xstream.entity.Address;
import com.xstream.entity.Person;
import org.xmlpull.mxp1.MXParser;
import java.io.ObjectInputStream;
import java.io.StringReader;
/**
* User: Rojer
* Date: 12-3-1
* Time: 下午4:48
* To change this template use File | Settings | File Templates.
*/
public class TestStream {
public static void main(String[] args) {
String res=toXML();
toEntity(res);
}
public static void toEntity(String inputXML){
XStream xs = new XStream();
// 这句和@XStreamAlias("person")等效
// xs.alias("person",Person.class);
// xs.alias("address",Address.class);
xs.setMode(XStream.NO_REFERENCES);
// 这句和@XStreamImplicit()等效
// xs.addImplicitCollection(Person.class,"addresses");
// 这句和@XStreamAsAttribute()
// xs.useAttributeFor(Person.class, "name");
//注册使用了注解的VO
xs.processAnnotations(new Class[]{Person.class,Address.class});
Person person = (Person)xs.fromXML(inputXML);
System.out.println(person.getAddresses().get(0).getHouseNo()+person.getName());
}
public static String toXML(){
XStream xStream = new XStream();
Person person = new Person();
person.setName("rojer");
person.setPhoneNuber(999);
Address address1 = new Address();
address1.setHouseNo(888);
address1.setStreet("newyork");
Address address2 = new Address();
address2.setHouseNo(76767);
address2.setStreet("toyo");
person.getAddresses().add(address1);
person.getAddresses().add(address2);
// xStream.alias("person", Person.class);
// xStream.alias("address",Address.class);
xStream.setMode(XStream.NO_REFERENCES);
// xStream.addImplicitCollection(Person.class, "addresses");
// xStream.useAttributeFor(Person.class,"name");
//注册使用了注解的VO
xStream.processAnnotations(new Class[]{Person.class,Address.class});
String xml = xStream.toXML(person);
System.out.println(xml);
return xml;
}
}
测试结果如下
<person name="rojer"> <phoneNuber>999</phoneNuber> <address> <street>newyork</street> <houseNo>888</houseNo> </address> <address> <street>toyo</street> <houseNo>76767</houseNo> </address> </person> 888rojer
如果去掉Person类的@XStreamImplicit()注解,生成的XML如下
<person name="rojer"> <phoneNuber>999</phoneNuber> <addresses> <address> <street>newyork</street> <houseNo>888</houseNo> </address> <address> <street>toyo</street> <houseNo>76767</houseNo> </address> </addresses> </person> 888rojer其字段addresses 会被写入到XML中。