---
title: "利用xstream处理java中的集合类"
author: "cjc"
date: "2006-12-10T12:24:00Z"
tags: []
slug: "xstream-java"
draft: false
lastmod: "2006-12-10T12:24:00Z"
id: 447011
url: "https://cjc.iteye.com/blog/447011"
---

利用xstream处理java中的集合类

越来越发现xstream的好用了，在处理很多麻烦事情时可以很简单的搞定，比如本来需要几个字段来保存的服务器配置信息，现在只需要写一个pojo表示服务器的config information，然后利用xstream将它变为xml，用一个字段就可以保存在数据库中，方便而且高效。  
但是在工作中，发现有时候pojo中需要用到java中的集合类，如ArrayList时，稍微有那么一点麻烦，今天特总结了一下，有不对之处希望大家批评指正。  
一.pojo中使用数组

1.先写一个表示通许录的pojo

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.xml;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\*\*通讯录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchb  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassAdressList...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringusername;//用户名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)String\[]telphone;//联系方式  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicString\[]getTelphone()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returntelphone;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetTelphone(String\[]telphone)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.telphone=telphone;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetUsername()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnusername;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetUsername(Stringusername)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.username=username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

2.利用xstream将一个

AdressList对象转化成xml

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)XStreamxstream=newXStream(newDomDriver());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)AdressListadressList=newAdressList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)adressList.setUsername("chb");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)String\[]tel=...{"13811071xxx","0391-6792xxx"};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)adressList.setTelphone(tel);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Stringobj\_xml=xstream.toXML(adressList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)System.out.println(obj\_xml);

3.输出结果为：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;chb.xml.AdressList&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;username&gt;chb&lt;/username&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;telphone&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;13811071xxx&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;0391-6792xxx&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/telphone&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/chb.xml.AdressList&gt;

4.如果觉得上面这段xml的root节点&lt;

chb.xml.AdressList&gt;不爽的话，可以利用xstream的annotations

来搞定。只需要改动两个地方即可：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.xml;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importcom.thoughtworks.xstream.annotations.XStreamAlias;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\*\*通讯录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchb  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
@XStreamAlias("adressList") ![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassAdressList...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringusername;//用户名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)String\[]telphone;//联系方式

然后在调用时更改如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)XStreamxstream=newXStream(newDomDriver());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Annotations.configureAliases(xstream,AdressList.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)AdressListadressList=newAdressList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)adressList.setUsername("chb");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)String\[]tel=...{"13811071xxx","0391-6792xxx"};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)adressList.setTelphone(tel);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Stringobj\_xml=xstream.toXML(adressList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)System.out.println(obj\_xml);

最后输出结果为：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;adressList&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;username&gt;chb&lt;/username&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;telphone&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;13811071xxx&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;string&gt;0391-6792xxx&lt;/string&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/telphone&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/adressList&gt;

关于xstream的

Annotations用法可以参考：http://xstream.codehaus.org/annotations-tutorial.html

二.pojo中使用ArrayList，ArrayList中装着另外的一个pojo

1.创建一个pojo

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagechb.xml;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.ArrayList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importcom.thoughtworks.xstream.annotations.XStreamAlias;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\*\*通讯录  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchb  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@XStreamAlias("adressList")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassAdressList...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringusername;//用户名  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ArrayList&lt;EmailAdress&gt;emailList;//电子邮件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetUsername()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnusername;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetUsername(Stringusername)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.username=username;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicArrayList&lt;EmailAdress&gt;getEmailList()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnemailList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetEmailList(ArrayList&lt;EmailAdress&gt;emailList)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.emailList=emailList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

2.emailList变量指向的

EmailAdress对象为：

packagechb.xml;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importcom.thoughtworks.xstream.annotations.XStreamAlias;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\*\*有点类似于javax.mail.internet.InternetAddress  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*如：chb&lt;hbcui1984@163.com&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchb  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)@XStreamAlias("email")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassEmailAdress...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringperson;//person  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringadress;//Adress  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetAdress()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnadress;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetAdress(Stringadress)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.adress=adress;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicStringgetPerson()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnperson;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidsetPerson(Stringperson)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.person=person;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

3.将一个

AdressList对象转化为对应的pojo

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)XStreamxstream=newXStream(newDomDriver());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Annotations.configureAliases(xstream,AdressList.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Annotations.configureAliases(xstream,EmailAdress.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)AdressListadressList=newAdressList();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)adressList.setUsername("chb");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)ArrayList&lt;EmailAdress&gt;emailList=newArrayList&lt;EmailAdress&gt;();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)for(inti=1;i&lt;4;i++)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)EmailAdressemail=newEmailAdress();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)email.setPerson("user"+i);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)email.setAdress("user"+i+"@163.com");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)emailList.add(email);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)adressList.setEmailList(emailList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)Stringobj\_xml=xstream.toXML(adressList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)System.out.println(obj\_xml);

4.输出结果为：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;adressList&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;username&gt;chb&lt;/username&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;emailList&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;email&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;person&gt;user1&lt;/person&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;adress&gt;user1@163.com&lt;/adress&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/email&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;email&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;person&gt;user2&lt;/person&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;adress&gt;user2@163.com&lt;/adress&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/email&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;email&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;person&gt;user3&lt;/person&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;adress&gt;user3@163.com&lt;/adress&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/email&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/emailList&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/adressList&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

三.用其他集合类的方法类似，这里就不举例了。