XStream对象相当Java对象和XML之间的转换器,转换过程是双向的。创建XSteam对象的方式很简单,只需要new XStream()即可。
Java到xml,用toXML()方法。
Xml到Java,用fromXML()方法。
在没有任何默认配置的情况下,java到xml的映射,是java成员名对应xml的元素名,java类的全名对应xml根元素的名字。而实际中,往往是xml和java类都有了,要完成相互转换,必须进行别名映射。
别名配置包含三种情况:
1、类别名,用alias(Stringname, Class type)。
2、类成员别名,用aliasField(Stringalias, Class definedIn, String fieldName)
3、类成员作为属性别名,用aliasAttribute(Class definedIn, String attributeName, String alias),单独命名没有意义,还要通过useAttributeFor(ClassdefinedIn, String fieldName) 应用到某个类上。
XStreamAnnotation
@XStreamAlias("Alias Name"): usedto alias Class name and field
@XStreamAsAttribute():add attribute for Class
@XStreamImplicit() 或者 @XStreamImplicit(itemFieldName = "Group")重新指定名字
XML解析器
1. XPP3 (需要xpp3-[version].jar)
XStream xstream = new XStream(new XppDriver());
2. JAXP DOM
XStream xstream = new XStream(new DomDriver()); 不指定编码解析器
XStream xstream = new XStream(newDomDriver(“UTF-8”)); 指定编码解析器
XStreamd的一些小bug
--在定义别名中的下划线“_”转换为xml后会变成“__”这个符号. 因此,尽量避免在别名中实用任何符号,却是需要下划线的时候,可以考虑实用连接符“-”,这个没有问题。
--Java Bean中常常有一些常量,在转换过程,XStream也会将这些常量转换过去,形成常量的xml节点,这显然不是想要的结果,对于常量字段,就不做转换了。
PersonBean.java:
/** *@author: chenyoulong *@Title:PersonBean.java *@date 2012-9-28 下午3:10:47 *@Description:TODO */ package com.entity; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; /** <person> <firstName>chen</firstName> <lastName>youlong</lastName> <telphone> <code>137280</code> <number>137280968</number> </telphone> <faxphone> <code>20</code> <number>020221327</number> </faxphone> <friends> <name>A1</name> <name>A2</name> <name>A3</name> </friends> <pets> <pet> <name>doly</name> <age>2</age> </pet> <pet> <name>Ketty</name> <age>2</age> </pet> </pets> </person> **/ /** * @ClassName:PersonBean * @author: chenyoulong Email: [email protected] * @date :2012-9-28 下午3:10:47 * @Description:TODO */ @XStreamAlias("person") public class PersonBean { @XStreamAlias("firstName") private String firstName; @XStreamAlias("lastName") private String lastName; @XStreamAlias("telphone") private PhoneNumber tel; @XStreamAlias("faxphone") private PhoneNumber fax; // 测试一个标签下有多个同名标签 @XStreamAlias("friends") private Friends friend; // 测试一个标签下循环对象 @XStreamAlias("pets") private Pets pet; 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 getTel() { return tel; } public void setTel(PhoneNumber tel) { this.tel = tel; } public PhoneNumber getFax() { return fax; } public void setFax(PhoneNumber fax) { this.fax = fax; } public Friends getFriend() { return friend; } public void setFriend(Friends friend) { this.friend = friend; } public Pets getPet() { return pet; } public void setPet(Pets pet) { this.pet = pet; } @XStreamAlias("phoneNumber") public static class PhoneNumber { @XStreamAlias("code") private int code; @XStreamAlias("number") 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; } } /** * 用Xstream注解的方式实现:一个标签下有多个同名标签 * * @ClassName:Friends * @author: chenyoulong Email: [email protected] * @date :2012-9-28 下午4:32:24 * @Description:TODO 5个name 中国,美国,俄罗斯,英国,法国 * http://blog.csdn.net/menhuanxiyou * /article/details/5426765 */ public static class Friends { @XStreamImplicit(itemFieldName = "name") // itemFieldName定义重复字段的名称, /* * <friends> <friends> <name>A1</name> <String>A1</String> * <name>A2</name> 如果没有,则会变成 =====> <String>A1</String> <name>A3</name> * <String>A1</String> </friends> </friends> */ private List<String> name; public List<String> getName() { return name; } public void setName(List<String> name) { this.name = name; } } // 测试同一标签下循环某一对象 public static class Animal { @XStreamAlias("name") private String name; @XStreamAlias("age") private int age; public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } /** * 测试同一标签下循环某一对象 * * @ClassName:Pets * @author: chenyoulong Email: [email protected] * @date :2012-9-28 下午6:26:01 * @Description:TODO */ public static class Pets { @XStreamImplicit(itemFieldName = "pet") private List<Animal> animalList; public List<Animal> getAnimalList() { return animalList; } public void setAnimalList(List<Animal> animalList) { this.animalList = animalList; } } }
public class PersonTest { /** * @Title: main * @Description: TODO * @param args * @return void */ public static void main(String[] args) { // TODO Auto-generated method stub // toXml // String xmlStr=new PersonTest().toXml(); // toBean // PersonBean per=new PersonTest().toBean(); String xmlStr = "<person>" + "<firstName>chen</firstName>" + "<lastName>youlong</lastName>" + "<telphone>" + "<code>137280</code>" + "<number>137280968</number>" + "</telphone>" + "<faxphone>" + "<code>20</code>" + "<number>020221327</number>" + "</faxphone>" + "<friends>" + "<name>A1</name>" + "<name>A2</name>" + "<name>A3</name>" + "</friends>" + "<pets>" + "<pet>" + "<name>doly</name>" + "<age>2</age>" + "</pet>" + "<pet>" + "<name>Ketty</name>" + "<age>2</age>" + "</pet>" + "</pets>" + "</person>"; PersonBean person = XmlUtil.toBean(xmlStr, PersonBean.class); System.out.println("person=firstname==" + person.getFirstName()); System.out.println("person==Friends==name1==" + person.getFriend().getName().get(0)); System.out.println("person==Pets==name2==" + person.getPet().getAnimalList().get(1).getName()); } }
转自:http://hi.baidu.com/readilychen/item/69ff3ab1b4d4df5f2aebe3e4
http://www.oschina.net/code/snippet_116183_14202