这些天要用到一些序列化相关知识,听人说XStream在序列化和反序列化xml方面很好,于是学了点他的api,写了个小例子:
先是一个用来序列化的类Parent:
package test.wei.tian.xstream; import java.util.List; import java.util.ArrayList; public class Parent { private String name; private String age; private List<Son> sons=new ArrayList<Son>(); /* * 写几个对应的getter和setter函数 */ public String getName(){ return name; } public void setName(String name) { this.name=name; } public String getAge(){ return age; } public void setAge(String age) { this.age=age; } public List getSons(){ return sons; } public void setSons(List sons){ this.sons=sons; } }
他里面用到了Son类,Son类如下:
package test.wei.tian.xstream; public class Son { public Son(String name, String grade) { super(); this.name = name; this.grade = grade; } public Son() { } private String name; private String grade; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } }
然后测试序列化以及反序列化:
package test.wei.tian.xstream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class TestXStream { private String tempStr=""; public String testSerialized(){ XStream xs=new XStream(); Son son1=new Son(); son1.setGrade("88"); son1.setName("Tom"); Son son2=new Son(); son2.setGrade("95"); son2.setName("Lilei"); Parent parent=new Parent(); parent.setAge("42"); parent.setName("Toris"); //将定义的Son放到Parent的链表中 parent.getSons().add(son1); parent.getSons().add(son2); //给类取别名。要不然会出现,因为生成xml时会自动填充为类名 //<test.wei.tian.xstream.Parent>而不是parent xs.alias("Parent", Parent.class); xs.alias("Son", Son.class); //将name,age设置为属性而不是子标记 xs.useAttributeFor(Parent.class, "name"); xs.useAttributeFor(Parent.class,"age"); //隐藏标记sons,因为Parent类有三个属性:name,age,sons。 //name和age都被设定为属性了,sons包括2个Son类型的变量. //会出现<sons><Son><name>xxx</name></Son><sons>,这样看着 //有些多余,故而隐藏sons这个标签对。 xs.addImplicitCollection(Parent.class, "sons"); //用XStream对象xs调用toXML函数,将对象Parent转化为xml,此即序列化 tempStr=xs.toXML(parent); //System.out.println(tempStr); return tempStr; } public void testDeserialized(){ String xml=testSerialized(); XStream xs=new XStream(new DomDriver()); Parent parent=new Parent(); xs.alias("Parent", Parent.class); xs.alias("Son", Son.class); xs.useAttributeFor(Parent.class,"name"); xs.useAttributeFor(Parent.class, "age"); xs.addImplicitCollection(Parent.class, "sons"); parent=(Parent)xs.fromXML(xml); System.out.println("Parent name is: "+parent.getName()); System.out.println("Parent Age is: "+parent.getAge()); List<Son> sons=new ArrayList<Son>(); sons=parent.getSons(); Iterator<Son> it=sons.iterator(); System.out.println(parent.getName()+" has "+sons.size()+" sons"); System.out.println("They are:"); while(it.hasNext()){ Son son=(Son)it.next(); System.out.println(son.getName()+". His grade is "+son.getGrade()); } } public static void main(String[]s){ TestXStream txs=new TestXStream(); String str=txs.testSerialized(); System.out.println("*****Serialized result********:"); System.out.println(str); System.out.println("*****Deserialized result:******"); txs.testDeserialized(); } }
不过为了方便没有写入和读取文件,只是直接用了String,Deserilized用的是Serilized的结果。
结果是:
*****Serialized result********: <Parent name="Toris" age="42"> <Son> <name>Tom</name> <grade>88</grade> </Son> <Son> <name>Lilei</name> <grade>95</grade> </Son> </Parent> *****Deserialized result:****** Parent name is: Toris Parent Age is: 42 Toris has 2 sons They are: Tom. His grade is 88 Lilei. His grade is 95