将对象转换为xml以持久化的圣器 xtream

本文介绍XStream框架,用于Java对象与XML之间的互相转换。包括基本使用方法、类及属性重命名技巧,以及如何序列化集合对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将对象转换为xml以持久化的圣器 xtream 



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代码   收藏代码
  1. package com.entity;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Student {  
  6.     private int id;  
  7.     private String name;  
  8.   
  9.     private String email;  
  10.     private String address;  
  11.     private Birthday birthday;  
  12.     private Date registDate;  
  13.       
  14.     public Date getRegistDate() {  
  15.         return registDate;  
  16.     }  
  17.   
  18.     public void setRegistDate(Date registDate) {  
  19.         this.registDate = registDate;  
  20.     }  
  21.   
  22.     public int getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(int id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.   
  38.     public String getEmail() {  
  39.         return email;  
  40.     }  
  41.   
  42.     public void setEmail(String email) {  
  43.         this.email = email;  
  44.     }  
  45.   
  46.     public String getAddress() {  
  47.         return address;  
  48.     }  
  49.   
  50.     public void setAddress(String address) {  
  51.         this.address = address;  
  52.     }  
  53.   
  54.     public Birthday getBirthday() {  
  55.         return birthday;  
  56.     }  
  57.   
  58.     public void setBirthday(Birthday birthday) {  
  59.         this.birthday = birthday;  
  60.     }  
  61.   
  62.     // getter、setter  
  63.     public String toString() {  
  64.         return this.name + "#" + this.id + "#" + this.address + "#"  
  65.                 + this.birthday + "#" + this.email;  
  66.     }  
  67. }  



7 bean 转成 xml 

测试代码: 
Java代码   收藏代码
  1. package com.test;  
  2.   
  3. import java.io.ObjectInputStream;  
  4. import java.io.ObjectOutputStream;  
  5. import java.util.Date;  
  6.   
  7. import org.junit.Before;  
  8. import org.junit.Test;  
  9.   
  10. import com.entity.Birthday;  
  11. import com.entity.Student;  
  12. import com.thoughtworks.xstream.XStream;  
  13.   
  14. @SuppressWarnings("unchecked")  
  15. public class XStreamTest {  
  16.   
  17.     private XStream xstream = null;  
  18.     private ObjectOutputStream out = null;  
  19.     private ObjectInputStream in = null;  
  20.   
  21.     private Student bean = null;  
  22.   
  23.     @Before  
  24.     public void init() {  
  25.         try {  
  26.             xstream = new XStream();  
  27.             bean = getTestStudent();  
  28.             // xstream = new XStream(new DomDriver()); // 需要xpp3 jar  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     public static void main(String[] args) {  
  35.         XStreamTest test = new XStreamTest();  
  36.         test.init();  
  37.         test.testWriteBean2XML_01();  
  38.     }  
  39.   
  40.     public final void fail(String string) {  
  41.         System.out.println(string);  
  42.     }  
  43.   
  44.     public final void failRed(String string) {  
  45.         System.err.println(string);  
  46.     }  
  47.   
  48.     /** 
  49.      * bean 2 XML 
  50.      * */  
  51.     @Test  
  52.     public void testWriteBean2XML_01() {  
  53.         try {  
  54.             fail("------------Bean->XML------------");  
  55.             fail(xstream.toXML(bean));  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.   
  61.     /** 
  62.      * 类重命名后的XML 
  63.      * */  
  64.     @Test  
  65.     public void testWriteBean2XML_02() {  
  66.         try {  
  67.             fail("-----------类重命名后的XML------------");  
  68.             // 类重命名  
  69.             xstream.alias("student", Student.class);  
  70.             xstream.aliasField("生日", Student.class"birthday");  
  71.             xstream.aliasField("生日日期", Birthday.class"birthday");  
  72.             fail(xstream.toXML(bean));  
  73.         } catch (Exception e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.     }  
  77.   
  78.     /** 
  79.      * 类重命名后的XML 
  80.      * */  
  81.     @Test  
  82.     public void testWriteBean2XML_03() {  
  83.         try {  
  84.             fail("-----------属性重命名后的XML------------");  
  85.             // 属性重命名  
  86.             xstream.aliasField("邮件", Student.class"email");  
  87.             fail(xstream.toXML(bean));  
  88.         } catch (Exception e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.     }  
  92.   
  93.     /** 
  94.      * 包重命名后的XML 
  95.      * */  
  96.     @Test  
  97.     public void testWriteBean2XML_04() {  
  98.         try {  
  99.             fail("-----------包重命名后的XML------------");  
  100.             //包重命名  
  101.             xstream.aliasPackage("modile""com.entity");  
  102.             fail(xstream.toXML(bean));  
  103.         } catch (Exception e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.     }  
  107.   
  108.     /** 
  109.      * 构造数据 
  110.      * */  
  111.     private Student getTestStudent() {  
  112.         Student bean = new Student();  
  113.         bean.setAddress("china");  
  114.         bean.setEmail("jack@email.com");  
  115.         bean.setId(1);  
  116.         bean.setName("jack");  
  117.         Birthday day = new Birthday();  
  118.         day.setBirthday("2010-11-22");  
  119.         bean.setBirthday(day);  
  120.         bean.setRegistDate(new Date());  
  121.         return bean;  
  122.     }  
  123. }  


测试结果: 
------------Bean->XML------------ 
<com.entity.Student> 
  <id>1</id> 
  <name>jack</name> 
  <email>jack@email.com</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------------ 
<student> 
  <id>1</id> 
  <name>jack</name> 
  <email>jack@email.com</email> 
  <address>china</address> 
  <生日> 
    <生日日期>2010-11-22</生日日期> 
  </生日> 
  <registDate>2011-07-11 22:33:02.390 CST</registDate> 
</student> 
-----------属性重命名后的XML------------ 
<com.entity.Student> 
  <id>1</id> 
  <name>jack</name> 
  <邮件>jack@email.com</邮件> 
  <address>china</address> 
  <birthday> 
    <birthday>2010-11-22</birthday> 
  </birthday> 
  <registDate>2011-07-11 22:33:02.406 CST</registDate> 
</com.entity.Student> 
-----------包重命名后的XML------------ 
<modile.Student> 
  <id>1</id> 
  <name>jack</name> 
  <email>jack@email.com</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 

Java代码   收藏代码
  1. fail("------------Listg<Strudent>->XML------------");  
  2. List<Student> list = new ArrayList<Student>();  
  3. list.add(bean);  
  4. Student s1 = getTestStudent();  
  5. s1.setId(2);  
  6. list.add(s1);  
  7. fail(xstream.toXML(list));  


结果: 
------------Listg<Strudent>->XML------------ 
<list> 
  <com.entity.Student> 
    <id>1</id> 
    <name>jack</name> 
    <email>jack@email.com</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>jack@email.com</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> 

  • 博客分类: 
  • java
 
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代码   收藏代码
  1. package com.entity;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Student {  
  6.     private int id;  
  7.     private String name;  
  8.   
  9.     private String email;  
  10.     private String address;  
  11.     private Birthday birthday;  
  12.     private Date registDate;  
  13.       
  14.     public Date getRegistDate() {  
  15.         return registDate;  
  16.     }  
  17.   
  18.     public void setRegistDate(Date registDate) {  
  19.         this.registDate = registDate;  
  20.     }  
  21.   
  22.     public int getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(int id) {  
  27.         this.id = id;  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name;  
  36.     }  
  37.   
  38.     public String getEmail() {  
  39.         return email;  
  40.     }  
  41.   
  42.     public void setEmail(String email) {  
  43.         this.email = email;  
  44.     }  
  45.   
  46.     public String getAddress() {  
  47.         return address;  
  48.     }  
  49.   
  50.     public void setAddress(String address) {  
  51.         this.address = address;  
  52.     }  
  53.   
  54.     public Birthday getBirthday() {  
  55.         return birthday;  
  56.     }  
  57.   
  58.     public void setBirthday(Birthday birthday) {  
  59.         this.birthday = birthday;  
  60.     }  
  61.   
  62.     // getter、setter  
  63.     public String toString() {  
  64.         return this.name + "#" + this.id + "#" + this.address + "#"  
  65.                 + this.birthday + "#" + this.email;  
  66.     }  
  67. }  



7 bean 转成 xml 

测试代码: 
Java代码   收藏代码
  1. package com.test;  
  2.   
  3. import java.io.ObjectInputStream;  
  4. import java.io.ObjectOutputStream;  
  5. import java.util.Date;  
  6.   
  7. import org.junit.Before;  
  8. import org.junit.Test;  
  9.   
  10. import com.entity.Birthday;  
  11. import com.entity.Student;  
  12. import com.thoughtworks.xstream.XStream;  
  13.   
  14. @SuppressWarnings("unchecked")  
  15. public class XStreamTest {  
  16.   
  17.     private XStream xstream = null;  
  18.     private ObjectOutputStream out = null;  
  19.     private ObjectInputStream in = null;  
  20.   
  21.     private Student bean = null;  
  22.   
  23.     @Before  
  24.     public void init() {  
  25.         try {  
  26.             xstream = new XStream();  
  27.             bean = getTestStudent();  
  28.             // xstream = new XStream(new DomDriver()); // 需要xpp3 jar  
  29.         } catch (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     public static void main(String[] args) {  
  35.         XStreamTest test = new XStreamTest();  
  36.         test.init();  
  37.         test.testWriteBean2XML_01();  
  38.     }  
  39.   
  40.     public final void fail(String string) {  
  41.         System.out.println(string);  
  42.     }  
  43.   
  44.     public final void failRed(String string) {  
  45.         System.err.println(string);  
  46.     }  
  47.   
  48.     /** 
  49.      * bean 2 XML 
  50.      * */  
  51.     @Test  
  52.     public void testWriteBean2XML_01() {  
  53.         try {  
  54.             fail("------------Bean->XML------------");  
  55.             fail(xstream.toXML(bean));  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.   
  61.     /** 
  62.      * 类重命名后的XML 
  63.      * */  
  64.     @Test  
  65.     public void testWriteBean2XML_02() {  
  66.         try {  
  67.             fail("-----------类重命名后的XML------------");  
  68.             // 类重命名  
  69.             xstream.alias("student", Student.class);  
  70.             xstream.aliasField("生日", Student.class"birthday");  
  71.             xstream.aliasField("生日日期", Birthday.class"birthday");  
  72.             fail(xstream.toXML(bean));  
  73.         } catch (Exception e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.     }  
  77.   
  78.     /** 
  79.      * 类重命名后的XML 
  80.      * */  
  81.     @Test  
  82.     public void testWriteBean2XML_03() {  
  83.         try {  
  84.             fail("-----------属性重命名后的XML------------");  
  85.             // 属性重命名  
  86.             xstream.aliasField("邮件", Student.class"email");  
  87.             fail(xstream.toXML(bean));  
  88.         } catch (Exception e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.     }  
  92.   
  93.     /** 
  94.      * 包重命名后的XML 
  95.      * */  
  96.     @Test  
  97.     public void testWriteBean2XML_04() {  
  98.         try {  
  99.             fail("-----------包重命名后的XML------------");  
  100.             //包重命名  
  101.             xstream.aliasPackage("modile""com.entity");  
  102.             fail(xstream.toXML(bean));  
  103.         } catch (Exception e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.     }  
  107.   
  108.     /** 
  109.      * 构造数据 
  110.      * */  
  111.     private Student getTestStudent() {  
  112.         Student bean = new Student();  
  113.         bean.setAddress("china");  
  114.         bean.setEmail("jack@email.com");  
  115.         bean.setId(1);  
  116.         bean.setName("jack");  
  117.         Birthday day = new Birthday();  
  118.         day.setBirthday("2010-11-22");  
  119.         bean.setBirthday(day);  
  120.         bean.setRegistDate(new Date());  
  121.         return bean;  
  122.     }  
  123. }  


测试结果: 
------------Bean->XML------------ 
<com.entity.Student> 
  <id>1</id> 
  <name>jack</name> 
  <email>jack@email.com</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------------ 
<student> 
  <id>1</id> 
  <name>jack</name> 
  <email>jack@email.com</email> 
  <address>china</address> 
  <生日> 
    <生日日期>2010-11-22</生日日期> 
  </生日> 
  <registDate>2011-07-11 22:33:02.390 CST</registDate> 
</student> 
-----------属性重命名后的XML------------ 
<com.entity.Student> 
  <id>1</id> 
  <name>jack</name> 
  <邮件>jack@email.com</邮件> 
  <address>china</address> 
  <birthday> 
    <birthday>2010-11-22</birthday> 
  </birthday> 
  <registDate>2011-07-11 22:33:02.406 CST</registDate> 
</com.entity.Student> 
-----------包重命名后的XML------------ 
<modile.Student> 
  <id>1</id> 
  <name>jack</name> 
  <email>jack@email.com</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 

Java代码   收藏代码
  1. fail("------------Listg<Strudent>->XML------------");  
  2. List<Student> list = new ArrayList<Student>();  
  3. list.add(bean);  
  4. Student s1 = getTestStudent();  
  5. s1.setId(2);  
  6. list.add(s1);  
  7. fail(xstream.toXML(list));  


结果: 
------------Listg<Strudent>->XML------------ 
<list> 
  <com.entity.Student> 
    <id>1</id> 
    <name>jack</name> 
    <email>jack@email.com</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>jack@email.com</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> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值