xtream 示例介绍
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt271
1 xStream框架
xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换;
官网:
http://xstream.codehaus.org/
2 about xtream
xtream 是一个简单的工具包,用来把对象序列化成xml配置文件,并且也可以把xml反序化成对象。
4Features 功能特点
简单易用,不需要配置映射,速度快并且占用内存小,生成的xml配置文件很干净,不带额外无用信息,这样在反映序列化的时候容易读取。不需要修改序列化对象的类型。支持类图。与xmlapi 整合。详细的返回错误信息。可修改的输出 显示。
4 点型应用
传输:网络传输
持久化:生成的XML可以写到文件,做持久化。
配置:XML最常用的配置文件。
单元测试
5局限
If using the enhanced mode, XStream can re-instantiate classes that do not have a default constructor. However, if using a different JVM like an old JRockit version, a JDK 1.3 or you have restrictions because of a SecurityManager, a default constructor is required.
The enhanced mode is also necessary to restore final fields for any JDK < 1.5. This implies deserialization of instances of an inner class.
Auto-detection of annotations may cause race conditions. Preprocessing annotations is safe though.
6准备一个pojo对象
Java代码
-
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
packagecom.entity;importjava.util.Date;bpublicclassStudent {privateintid;privateString name;privateString email;privateString address;privateBirthday birthday;privateDate registDate;publicDate getRegistDate() {returnregistDate;}publicvoidsetRegistDate(Date registDate) {this.registDate = registDate;}publicintgetId() {returnid;}publicvoidsetId(intid) {this.id = id;}publicString getName() {returnname;}publicvoidsetName(String name) {this.name = name;}publicString getEmail() {returnemail;}publicvoidsetEmail(String email) {this.email = email;}publicString getAddress() {returnaddress;}publicvoidsetAddress(String address) {this.address = address;}publicBirthday getBirthday() {returnbirthday;}publicvoidsetBirthday(Birthday birthday) {this.birthday = birthday;}// getter、setterpublicString toString() {returnthis.name +"#"+this.id +"#"+this.address +"#"+this.birthday +"#"+this.email;}}
7 bean 转成 xml
测试代码:
Java代码
-
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
packagecom.test;importjava.io.ObjectInputStream;importjava.io.ObjectOutputStream;importjava.util.Date;importorg.junit.Before;importorg.junit.Test;importcom.entity.Birthday;importcom.entity.Student;importcom.thoughtworks.xstream.XStream;@SuppressWarnings("unchecked")publicclassXStreamTest {privateXStream xstream =null;privateObjectOutputStream out =null;privateObjectInputStream in =null;privateStudent bean =null;@Beforepublicvoidinit() {try{xstream =newXStream();bean = getTestStudent();// xstream = new XStream(new DomDriver()); // 需要xpp3 jar}catch(Exception e) {e.printStackTrace();}}publicstaticvoidmain(String[] args) {XStreamTest test =newXStreamTest();test.init();test.testWriteBean2XML_01();}publicfinalvoidfail(String string) {System.out.println(string);}publicfinalvoidfailRed(String string) {System.err.println(string);}/*** bean 2 XML* */@TestpublicvoidtestWriteBean2XML_01() {try{fail("------------Bean->XML------------");fail(xstream.toXML(bean));}catch(Exception e) {e.printStackTrace();}}/*** 类重命名后的XML* */@TestpublicvoidtestWriteBean2XML_02() {try{fail("-----------类重命名后的XML------------");// 类重命名xstream.alias("student", Student.class);xstream.aliasField("生日", Student.class,"birthday");xstream.aliasField("生日日期", Birthday.class,"birthday");fail(xstream.toXML(bean));}catch(Exception e) {e.printStackTrace();}}/*** 类重命名后的XML* */@TestpublicvoidtestWriteBean2XML_03() {try{fail("-----------属性重命名后的XML------------");// 属性重命名xstream.aliasField("邮件", Student.class,"email");fail(xstream.toXML(bean));}catch(Exception e) {e.printStackTrace();}}/*** 包重命名后的XML* */@TestpublicvoidtestWriteBean2XML_04() {try{fail("-----------包重命名后的XML------------");//包重命名xstream.aliasPackage("modile","com.entity");fail(xstream.toXML(bean));}catch(Exception e) {e.printStackTrace();}}/*** 构造数据* */privateStudent getTestStudent() {Student bean =newStudent();bean.setAddress("china");bean.setEmail("email");bean.setId(1);bean.setName("jack");Birthday day =newBirthday();day.setBirthday("2010-11-22");bean.setBirthday(day);bean.setRegistDate(newDate());returnbean;}}
测试结果:
------------Bean->XML------------
|
1
2
3
4
5
6
7
8
9
10
|
<com.entity.Student> <id>1</id> <name>jack</name> <email>email</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> <registDate>2011-07-11 22:33:02.359 CST</registDate> </com.entity.Student> |
-----------类重命名后的XML------------
|
1
2
3
4
5
6
7
8
9
10
|
<student> <id>1</id> <name>jack</name> <email>email</email> <address>china</address> <生日> <生日日期>2010-11-22</生日日期> </生日> <registDate>2011-07-11 22:33:02.390 CST</registDate> </student> |
-----------属性重命名后的XML------------
|
1
2
3
4
5
6
7
8
9
10
|
<com.entity.Student> <id>1</id> <name>jack</name> <邮件>email</邮件> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> <registDate>2011-07-11 22:33:02.406 CST</registDate> </com.entity.Student> |
-----------包重命名后的XML------------
|
1
2
3
4
5
6
7
8
9
10
|
<modile.Student> <id>1</id> <name>jack</name> <email>e</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> <registDate>2011-07-11 22:33:02.406 CST</registDate> </modile.Student> |
8 List 2 XML
-
-
1234567
fail("------------Listg<Strudent>->XML------------");List<Student> list =newArrayList<Student>();list.add(bean);Student s1 = getTestStudent();s1.setId(2);list.add(s1);fail(xstream.toXML(list));
结果:
------------Listg<Strudent>->XML------------
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<list> <com.entity.Student> <id>1</id> <name>jack</name> <email>email</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> <registDate>2011-07-11 22:47:08.0 CST</registDate> </com.entity.Student> <com.entity.Student> <id>2</id> <name>jack</name> <email>email</email> <address>china</address> <birthday> <birthday>2010-11-22</birthday> </birthday> <registDate>2011-07-11 22:47:08.0 CST</registDate> </com.entity.Student> </list>
|
浙公网安备 33010602011771号