XStream是一个简单的类库,可以序列化对象到xml,还可以将xml还原为对象。

XStream官网: http://xstream.codehaus.org/

附件提供XStream和xpp3相关的jar下载:

  • xstream-1.2.2.jar
  • xpp3-1.1.3.3_min.jar
  • 为了使用XStream,需要对其初始化,初始化方法有两种:

    方法一:直接初始化XStream

    XStream xstream = new XStream();

    这种方式的初始化需要xpp3-[version].jar的支持。xpp是一个快速解析XML文件的解析器。

    方法二:依赖XPP3.jar的支持,使用标准的JAXP DOM来解析

    应网友的要求增加了数组对象的序列化测试,不过我测试时没有设置mode参数也可以正常序列化

    同时这里对mode参数做个简单的说明:

    在XStream序列化或反序列化对象时,它会创建两个类MarshallingContext和UnmarshallingContext,由
    它们来处理数据,以及委派合适的转换器。XStream提供了三对上下文的缺省实现,它们之间有着细微的差别。缺省值可以通过方法
    XStream.setMode()来改变,需要传递下面参数中的一个:

  • XStream.XPATH_RELATIVE_REFERENCES :(缺省)通过XPath引用来标识重复的引用,使用相对路径表示。
  • XStream.XPATH_ABSOLUTE_REFERENCES :通过XPath引用来标识重复的引用,使用绝对路径表示。
  • XStream.ID_REFERENCES :使用ID引用来标识重复的引用。在一些场合(手写XML时),将会更易于操作
  • XStream.NO_REFERENCES :这种情况将失去对图形对象的支持,仅把对象看作为树型结构。重复的引用被视作两个不同的对象,循环引用会导致异常产生。相对于上面两种模式,这种模式速度会更快,占用内存会更
  • 下面简单的例子包括三个类:UserBean.java,ContactBean.java,XstreamTestMain.java代码如下:

    xstream测试类: XstreamTestMain.java

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; * Xtream 序列化及反序列化 * @blog http://sjsky.iteye.com * @author Michael public class XtreamTestMain { * @param args public static void main(String[] args) { UserBean userBean = initBean(); // 依懒xpp解析器 // XStream xStream = new XStream(); // 运用标准的DOM解析器 // XStream xStream = new XStream(new DomDriver()); // 指定编码格式 XStream xStream = new XStream(new DomDriver("utf-8")); // xStream.setMode(XStream.NO_REFERENCES); // 直接指定修改生成XML的节点名称 xStream.alias("UserBean", UserBean.class); String xml = xStream.toXML(userBean); System.out.println("BEAN转化为XML 字符串-----------------------------"); System.out.println(xml); System.out.println("XML转化为BEAN-----------------------------"); UserBean parseBean = (UserBean) xStream.fromXML(xml); System.out.println("userName:" + parseBean.getUserName()); System.out.println("age:" + parseBean.getAge()); System.out.println("birthday:" + parseBean.getBirthday()); for (String str : parseBean.getAddresList()) { System.out.println("addres:" + str); for (Entry<String, String> entry : parseBean.getNickNameMap() .entrySet()) { System.out.println("nick name:" + entry.getKey() + "<->" + entry.getValue()); ContactBean contact = parseBean.getContact(); System.out.println("postcode:" + contact.getPostCode()); System.out.println("telephone:" + contact.getTelephone()); System.out.println("mobile:" + contact.getMobile()); System.out.println("email:" + contact.getEmail()); System.out.println("BEAN转化为XML文件-----------------------------"); try { FileOutputStream fos = new FileOutputStream("d:/test/userbean.xml"); xStream.toXML(userBean, fos); } catch (Exception ex) { ex.printStackTrace(); System.out.println("XML文件转化为BEAN-----------------------------"); UserBean parseFileBean = new UserBean(); try { FileInputStream fis = new FileInputStream("d:/test/userbean.xml"); parseFileBean = (UserBean) xStream.fromXML(fis, parseFileBean); System.out.println("parseFileBean->" + parseFileBean); } catch (Exception ex) { ex.printStackTrace(); * @return test bean private static UserBean initBean() { ContactBean contact = new ContactBean(); contact.setPostCode("200000"); contact.setTelephone("021-88888888"); contact.setMobile("1891800xxxx"); contact.setEmail("[email protected]"); List<String> addressList = new ArrayList<String>(); addressList.add("上海杨浦"); addressList.add("江苏苏州"); Map<String, String> nickNameMap = new HashMap<String, String>(); nickNameMap.put("大大", "小小"); nickNameMap.put("老公", "老婆"); UserBean userBean = new UserBean(); userBean.setUserName("michael"); userBean.setAge(26); userBean.setBirthday(new Date()); userBean.setAddresList(addressList); userBean.setNickNameMap(nickNameMap); userBean.setContact(contact); ContactBean contact1 = new ContactBean(); contact1.setPostCode("200011"); contact1.setTelephone("021-11111111"); contact1.setMobile("13911111111"); contact1.setEmail("[email protected]"); ContactBean contact2 = new ContactBean(); contact2.setPostCode("20002"); contact2.setTelephone("021-22222222"); contact2.setMobile("13922222222"); contact2.setEmail("[email protected]"); ContactBean[] contactArr = new ContactBean[] { contact1, contact2 }; userBean.setContactArr(contactArr); return userBean;
    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
    121
    122
    123
    124
    125
    126
    package michael . xstream ;
    import java . io . FileInputStream ;
    import java . io . FileOutputStream ;
    import java . util . ArrayList ;
    import java . util . Date ;
    import java . util . HashMap ;
    import java . util . List ;
    import java . util . Map ;
    import java . util . Map . Entry ;
    import com . thoughtworks . xstream . XStream ;
    import com . thoughtworks . xstream . io . xml . DomDriver ;
    /**
    * Xtream 序列化及反序列化
    * @blog http://sjsky.iteye.com
    * @author Michael
    */
    public class XtreamTestMain {
    /**
    * @param args
    */
    public static void main ( String [ ] args ) {
    UserBean userBean = initBean ( ) ;
    // 依懒xpp解析器
    // XStream xStream = new XStream();
    // 运用标准的DOM解析器
    // XStream xStream = new XStream(new DomDriver());
    // 指定编码格式
    XStream xStream = new XStream ( new DomDriver ( "utf-8" ) ) ;
    // xStream.setMode(XStream.NO_REFERENCES);
    // 直接指定修改生成XML的节点名称
    xStream . alias ( "UserBean" , UserBean . class ) ;
    String xml = xStream . toXML ( userBean ) ;
    System . out . println ( "BEAN转化为XML 字符串-----------------------------" ) ;
    System . out . println ( xml ) ;
    System . out . println ( "XML转化为BEAN-----------------------------" ) ;
    UserBean parseBean = ( UserBean ) xStream . fromXML ( xml ) ;
    System . out . println ( "userName:" + parseBean . getUserName ( ) ) ;
    System . out . println ( "age:" + parseBean . getAge ( ) ) ;
    System . out . println ( "birthday:" + parseBean . getBirthday ( ) ) ;
    for ( String str : parseBean . getAddresList ( ) ) {
    System . out . println ( "addres:" + str ) ;
    }
    for ( Entry < String , String > entry : parseBean . getNickNameMap ( )
    . entrySet ( ) ) {
    System . out . println ( "nick name:" + entry . getKey ( ) + "<->"
    + entry . getValue ( ) ) ;
    }
    ContactBean contact = parseBean . getContact ( ) ;
    System . out . println ( "postcode:" + contact . getPostCode ( ) ) ;
    System . out . println ( "telephone:" + contact . getTelephone ( ) ) ;
    System . out . println ( "mobile:" + contact . getMobile ( ) ) ;
    System . out . println ( "email:" + contact . getEmail ( ) ) ;
    System . out . println ( "BEAN转化为XML文件-----------------------------" ) ;
    try {
    FileOutputStream fos = new FileOutputStream ( "d:/test/userbean.xml" ) ;
    xStream . toXML ( userBean , fos ) ;
    } catch ( Exception ex ) {
    ex . printStackTrace ( ) ;
    }
    System . out . println ( "XML文件转化为BEAN-----------------------------" ) ;
    UserBean parseFileBean = new UserBean ( ) ;
    try {
    FileInputStream fis = new FileInputStream ( "d:/test/userbean.xml" ) ;
    parseFileBean = ( UserBean ) xStream . fromXML ( fis , parseFileBean ) ;
    System . out . println ( "parseFileBean->" + parseFileBean ) ;
    } catch ( Exception ex ) {
    ex . printStackTrace ( ) ;
    }
    }
    /**
    * @return test bean
    */
    private static UserBean initBean ( ) {
    ContactBean contact = new ContactBean ( ) ;
    contact . setPostCode ( "200000" ) ;
    contact . setTelephone ( "021-88888888" ) ;
    contact . setMobile ( "1891800xxxx" ) ;
    contact . setEmail ( "[email protected]" ) ;
    List < String > addressList = new ArrayList < String > ( ) ;
    addressList . add ( "上海杨浦" ) ;
    addressList . add ( "江苏苏州" ) ;
    Map < String , String > nickNameMap = new HashMap < String , String > ( ) ;
    nickNameMap . put ( "大大" , "小小" ) ;
    nickNameMap . put ( "老公" , "老婆" ) ;
    UserBean userBean = new UserBean ( ) ;
    userBean . setUserName ( "michael" ) ;
    userBean . setAge ( 26 ) ;
    userBean . setBirthday ( new Date ( ) ) ;
    userBean . setAddresList ( addressList ) ;
    userBean . setNickNameMap ( nickNameMap ) ;
    userBean . setContact ( contact ) ;
    ContactBean contact1 = new ContactBean ( ) ;
    contact1 . setPostCode ( "200011" ) ;
    contact1 . setTelephone ( "021-11111111" ) ;
    contact1 . setMobile ( "13911111111" ) ;
    contact1 . setEmail ( "[email protected]" ) ;
    ContactBean contact2 = new ContactBean ( ) ;
    contact2 . setPostCode ( "20002" ) ;
    contact2 . setTelephone ( "021-22222222" ) ;
    contact2 . setMobile ( "13922222222" ) ;
    contact2 . setEmail ( "[email protected]" ) ;
    ContactBean [ ] contactArr = new ContactBean [ ] { contact1 , contact2 } ;
    userBean . setContactArr ( contactArr ) ;
    return userBean ;
    }
    }
    * @param pAddresList the addresList to set public void setAddresList(List<String> pAddresList) { addresList = pAddresList; * @param pNickNameMap the nickNameMap to set public void setNickNameMap(Map<String, String> pNickNameMap) { nickNameMap = pNickNameMap; * @param pContact the contact to set public void setContact(ContactBean pContact) { contact = pContact; * @return the contactArr public ContactBean[] getContactArr() { return contactArr; * @param pContactArr the contactArr to set public void setContactArr(ContactBean[] pContactArr) { contactArr = pContactArr;
    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
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    package michael . xstream ;
    import java . util . ArrayList ;
    import java . util . Date ;
    import java . util . HashMap ;
    import java . util . List ;
    import java . util . Map ;
    /**
    * @author Michael
    */
    public class UserBean {
    /**
    * 用户名称
    */
    private String userName ;
    /**
    * 年龄
    */
    private Integer age ;
    /**
    * 出生日期
    */
    private Date birthday ;
    /**
    * 地址
    */
    private List < String > addresList = new ArrayList < String > ( ) ;
    /**
    * 昵称
    */
    private Map < String , String > nickNameMap = new HashMap < String , String > ( ) ;
    /**
    * 联系方式Bean
    */
    private ContactBean contact ;
    /**
    * 测试对象数组
    */
    private ContactBean [ ] contactArr ;
    /**
    * @return the userName
    */
    public String getUserName ( ) {
    return userName ;
    }
    /**
    * @return the age
    */
    public Integer getAge ( ) {
    return age ;
    }
    /**
    * @return the birthday
    */
    public Date getBirthday ( ) {
    return birthday ;
    }
    /**
    * @return the addresList
    */
    public List < String > getAddresList ( ) {
    return addresList ;
    }
    /**
    * @return the nickNameMap
    */
    public Map < String , String > getNickNameMap ( ) {
    return nickNameMap ;
    }
    /**
    * @return the contact
    */
    public ContactBean getContact ( ) {
    return contact ;
    }
    /**
    * @param pUserName the userName to set
    */
    public void setUserName ( String pUserName ) {
    userName = pUserName ;
    }
    /**
    * @param pAge the age to set
    */
    public void setAge ( Integer pAge ) {
    age = pAge ;
    }
    /**
    * @param pBirthday the birthday to set
    */
    public void setBirthday ( Date pBirthday ) {
    birthday = pBirthday ;
    }
    /**
    * @param pAddresList the addresList to set
    */
    public void setAddresList ( List < String > pAddresList ) {
    addresList = pAddresList ;
    }
    /**
    * @param pNickNameMap the nickNameMap to set
    */
    public void setNickNameMap ( Map < String , String > pNickNameMap ) {
    nickNameMap = pNickNameMap ;
    }
    /**
    * @param pContact the contact to set
    */
    public void setContact ( ContactBean pContact ) {
    contact = pContact ;
    }
    /**
    * @return the contactArr
    */
    public ContactBean [ ] getContactArr ( ) {
    return contactArr ;
    }
    /**
    * @param pContactArr the contactArr to set
    */
    public void setContactArr ( ContactBean [ ] pContactArr ) {
    contactArr = pContactArr ;
    }
    }
    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
    package com . test ;
    /**
    * @author Michael
    */
    public class ContactBean {
    /**
    * 邮编
    */
    private String postCode ;
    /**
    * 电话
    */
    private String telephone ;
    /**
    * 手机
    */
    private String mobile ;
    /**
    * email
    */
    private String email ;
    /**
    * @return the postCode
    */
    public String getPostCode ( ) {
    return postCode ;
    }
    /**
    * @return the telephone
    */
    public String getTelephone ( ) {
    return telephone ;
    }
    /**
    * @return the mobile
    */
    public String getMobile ( ) {
    return mobile ;
    }
    /**
    * @return the email
    */
    public String getEmail ( ) {
    return email ;
    }
    /**
    * @param pPostCode the postCode to set
    */
    public void setPostCode ( String pPostCode ) {
    postCode = pPostCode ;
    }
    /**
    * @param pTelephone the telephone to set
    */
    public void setTelephone ( String pTelephone ) {
    telephone = pTelephone ;
    }
    /**
    * @param pMobile the mobile to set
    */
    public void setMobile ( String pMobile ) {
    mobile = pMobile ;
    }
    /**
    * @param pEmail the email to set
    */
    public void setEmail ( String pEmail ) {
    email = pEmail ;
    }
    }
    <telephone>021-11111111</telephone> <mobile>13911111111</mobile> <email>[email protected]</email> </michael.xstream.ContactBean> <michael.xstream.ContactBean> <postCode>20002</postCode> <telephone>021-22222222</telephone> <mobile>13922222222</mobile> <email>[email protected]</email> </michael.xstream.ContactBean> </contactArr> </UserBean> XML转化为BEAN----------------------------- userName:michael age:26 birthday:Wed Jul 20 23:33:32 CST 2011 addres:上海杨浦 addres:江苏苏州 nick name:老公<->老婆 nick name:大大<->小小 postcode:200000 telephone:021-88888888 mobile:1891800xxxx email:[email protected] BEAN转化为XML文件----------------------------- XML文件转化为BEAN----------------------------- parseFileBean->michael.xstream.UserBean@bf7190
    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
    BEAN 转化为 XML 字符串 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    < UserBean >
    < userName > michael < / userName >
    < age > 26 < / age >
    < birthday > 2011 - 07 - 20 23 : 33 : 32.969 CST < / birthday >
    < addresList >
    < string > 上海杨浦 < / string >
    < string > 江苏苏州 < / string >
    < / addresList >
    < nickNameMap >
    < entry >
    < string > 老公 < / string >
    < string > 老婆 < / string >
    < / entry >
    < entry >
    < string > 大大 < / string >
    < string > 小小 < / string >
    < / entry >
    < / nickNameMap >
    < contact >
    < postCode > 200000 < / postCode >
    < telephone > 021 - 88888888 < / telephone >
    < mobile > 1891800xxxx < / mobile >
    < email > test @ test . com < / email >
    < / contact >
    < contactArr >
    < michael . xstream . ContactBean >
    < postCode > 200011 < / postCode >
    < telephone > 021 - 11111111 < / telephone >
    < mobile > 13911111111 < / mobile >
    < email > contact1 @ test . com < / email >
    < / michael . xstream . ContactBean >
    < michael . xstream . ContactBean >
    < postCode > 20002 < / postCode >
    < telephone > 021 - 22222222 < / telephone >
    < mobile > 13922222222 < / mobile >
    < email > contact2 @ test . com < / email >
    < / michael . xstream . ContactBean >
    < / contactArr >
    < / UserBean >
    XML 转化为 BEAN -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    userName : michael
    age : 26
    birthday : Wed Jul 20 23 : 33 : 32 CST 2011
    addres : 上海杨浦
    addres : 江苏苏州
    nick name : 老公 < -> 老婆
    nick name : 大大 < -> 小小
    postcode : 200000
    telephone : 021 - 88888888
    mobile : 1891800xxxx
    email : test @ test . com
    BEAN 转化为 XML 文件 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    XML 文件转化为 BEAN -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    parseFileBean -> michael . xstream . UserBean @ bf7190
    <telephone>021-11111111</telephone> <mobile>13911111111</mobile> <email>[email protected]</email> </michael.xstream.ContactBean> <michael.xstream.ContactBean> <postCode>20002</postCode> <telephone>021-22222222</telephone> <mobile>13922222222</mobile> <email>[email protected]</email> </michael.xstream.ContactBean> </contactArr> </UserBean>
    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
    < UserBean >
    < userName > michael < / userName >
    < age > 26 < / age >
    < birthday > 2011 - 07 - 20 23 : 33 : 32.969 CST < / birthday >
    < addresList >
    < string > 上海杨浦 < / string >
    < string > 江苏苏州 < / string >
    < / addresList >
    < nickNameMap >
    < entry >
    < string > 老公 < / string >
    < string > 老婆 < / string >
    < / entry >
    < entry >
    < string > 大大 < / string >
    < string > 小小 < / string >
    < / entry >
    < / nickNameMap >
    < contact >
    < postCode > 200000 < / postCode >
    < telephone > 021 - 88888888 < / telephone >
    < mobile > 1891800xxxx < / mobile >
    < email > test @ test . com < / email >
    < / contact >
    < contactArr >
    < michael . xstream . ContactBean >
    < postCode > 200011 < / postCode >
    < telephone > 021 - 11111111 < / telephone >
    < mobile > 13911111111 < / mobile >
    < email > contact1 @ test . com < / email >
    < / michael . xstream . ContactBean >
    < michael . xstream . ContactBean >
    < postCode > 20002 < / postCode >
    < telephone > 021 - 22222222 < / telephone >
    < mobile > 13922222222 < / mobile >
    < email > contact2 @ test . com < / email >
    < / michael . xstream . ContactBean >
    < / contactArr >
    < / UserBean >