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




7 bean 转成 xml 

测试代码: 

Java代码

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



测试结果: 
------------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 

Java代码  收藏代码

  1. 1
    2
    3
    4
    5
    6
    7
    fail( "------------Listg<Strudent>->XML------------" );  
    List<Student> list =  new  ArrayList<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 >
特别说明:尊重作者的劳动成果,转载请注明出处哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt271

相关文章
一个完整的 ParentDataWidget 示例
一个完整的 ParentDataWidget 示例
79 0
一个完整的 ParentDataWidget 示例
|
关系型数据库 Java 数据库连接
SpringMyBatis解析1-使用示例
MyBatis使用介绍   MyBatis的详细使用介绍  http://www.cnblogs.com/xrq730/category/796495.html   建立PO  public class Person { private String id; private String name; //set get 方法、、、 } 建立Mapper mapper是数据库操作的映射文件,也就是我们常说的dao文件。
937 0
|
Java Spring 网络安全
SpringHttpInvoker解析1-使用示例
HTTP invoker是一个新的远程调用模型,作为Spring框架的一部分,来执行基于HTTP的远程调用(让防火墙可以接受),并使用Java的序列化机制。 服务端 定义服务接口UserService import java.
810 0
|
Android开发 数据格式 XML
ActivityLifecycleCallbacks使用示例
MyApplication如下: package com.cc; import java.util.LinkedList; import android.
827 0
|
Python C语言
练习-libev和pyev示例
事件循环,IO复用,还是理解深刻一点好。 比较LIBEV和PYEV,发现PYTHON库只是对LIBEV作了简单的语法转换。 到了这个层次,就一个字:DIAO!!! libev的C版: #include #include ev_io stdin_watcher; ev_tim...
1130 0