利用xstream处理java中的集合类
越来越发现xstream的好用了,在处理很多麻烦事情时可以很简单的搞定,比如本来需要几个字段来保存的服务器配置信息,现在只需要写一个pojo表示服务器的config information,然后利用xstream将它变为xml,用一个字段就可以保存在数据库中,方便而且高效。但是在工作中,发现有时候pojo中需要用到java中的集合类,如ArrayList时,稍微有那么一点麻烦,今天特总结了一下,有不对之处希望大家批评指正。
一.pojo中使用数组
1.先写一个表示通许录的pojo
2.利用xstream将一个AdressList对象转化成xml
3.输出结果为:
4.如果觉得上面这段xml的root节点<chb.xml.AdressList>不爽的话,可以利用xstream的annotations来搞定。只需要改动两个地方即可:
然后在调用时更改如下:
最后输出结果为:
关于xstream的Annotations用法可以参考:http://xstream.codehaus.org/annotations-tutorial.html
二.pojo中使用ArrayList,ArrayList中装着另外的一个pojopackage chb.xml;
/**通讯录
* @author chb
*
*/
public class AdressList {
String username;//用户名
String[] telphone;//联系方式
public String[] getTelphone() {
return telphone;
}
public void setTelphone(String[] telphone) {
this.telphone = telphone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
/**通讯录
* @author chb
*
*/
public class AdressList {
String username;//用户名
String[] telphone;//联系方式
public String[] getTelphone() {
return telphone;
}
public void setTelphone(String[] telphone) {
this.telphone = telphone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
XStream xstream = new XStream(new DomDriver());
AdressList adressList=new AdressList();
adressList.setUsername("chb");
String[] tel={"13811071xxx","0391-6792xxx"};
adressList.setTelphone(tel);
String obj_xml=xstream.toXML(adressList);
System.out.println(obj_xml);
AdressList adressList=new AdressList();
adressList.setUsername("chb");
String[] tel={"13811071xxx","0391-6792xxx"};
adressList.setTelphone(tel);
String obj_xml=xstream.toXML(adressList);
System.out.println(obj_xml);
<chb.xml.AdressList>
<username>chb</username>
<telphone>
<string>13811071xxx</string>
<string>0391-6792xxx</string>
</telphone>
</chb.xml.AdressList>
<username>chb</username>
<telphone>
<string>13811071xxx</string>
<string>0391-6792xxx</string>
</telphone>
</chb.xml.AdressList>
package chb.xml;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**通讯录
* @author chb
*
*/
@XStreamAlias("adressList")
public class AdressList {
String username;//用户名
String[] telphone;//联系方式
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**通讯录
* @author chb
*
*/
@XStreamAlias("adressList")
public class AdressList {
String username;//用户名
String[] telphone;//联系方式
XStream xstream = new XStream(new DomDriver());
Annotations.configureAliases(xstream, AdressList.class);
AdressList adressList=new AdressList();
adressList.setUsername("chb");
String[] tel={"13811071xxx","0391-6792xxx"};
adressList.setTelphone(tel);
String obj_xml=xstream.toXML(adressList);
System.out.println(obj_xml);
Annotations.configureAliases(xstream, AdressList.class);
AdressList adressList=new AdressList();
adressList.setUsername("chb");
String[] tel={"13811071xxx","0391-6792xxx"};
adressList.setTelphone(tel);
String obj_xml=xstream.toXML(adressList);
System.out.println(obj_xml);
<adressList>
<username>chb</username>
<telphone>
<string>13811071xxx</string>
<string>0391-6792xxx</string>
</telphone>
</adressList>
<username>chb</username>
<telphone>
<string>13811071xxx</string>
<string>0391-6792xxx</string>
</telphone>
</adressList>
1.创建一个pojo
2.emailList变量指向的EmailAdress对象为:
3.将一个AdressList对象转化为对应的pojo
4.输出结果为:
三.用其他集合类的方法类似,这里就不举例了。package chb.xml;
import java.util.ArrayList;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**通讯录
* @author chb
*
*/
@XStreamAlias("adressList")
public class AdressList {
String username;//用户名
ArrayList<EmailAdress> emailList;//电子邮件
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public ArrayList<EmailAdress> getEmailList() {
return emailList;
}
public void setEmailList(ArrayList<EmailAdress> emailList) {
this.emailList = emailList;
}
}
import java.util.ArrayList;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**通讯录
* @author chb
*
*/
@XStreamAlias("adressList")
public class AdressList {
String username;//用户名
ArrayList<EmailAdress> emailList;//电子邮件
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public ArrayList<EmailAdress> getEmailList() {
return emailList;
}
public void setEmailList(ArrayList<EmailAdress> emailList) {
this.emailList = emailList;
}
}
package chb.xml;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**有点类似于javax.mail.internet.InternetAddress
*
* 如:chb <hbcui1984@163.com>
*
* @author chb
*
*/
@XStreamAlias("email")
public class EmailAdress {
String person;//person
String adress;//Adress
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**有点类似于javax.mail.internet.InternetAddress
*
* 如:chb <hbcui1984@163.com>
*
* @author chb
*
*/
@XStreamAlias("email")
public class EmailAdress {
String person;//person
String adress;//Adress
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
}
XStream xstream = new XStream(new DomDriver());
Annotations.configureAliases(xstream, AdressList.class);
Annotations.configureAliases(xstream, EmailAdress.class);
AdressList adressList=new AdressList();
adressList.setUsername("chb");
ArrayList<EmailAdress> emailList=new ArrayList<EmailAdress>();
for(int i=1;i<4;i++){
EmailAdress email=new EmailAdress();
email.setPerson("user"+i);
email.setAdress("user"+i+"@163.com");
emailList.add(email);
}
adressList.setEmailList(emailList);
String obj_xml=xstream.toXML(adressList);
System.out.println(obj_xml);
Annotations.configureAliases(xstream, AdressList.class);
Annotations.configureAliases(xstream, EmailAdress.class);
AdressList adressList=new AdressList();
adressList.setUsername("chb");
ArrayList<EmailAdress> emailList=new ArrayList<EmailAdress>();
for(int i=1;i<4;i++){
EmailAdress email=new EmailAdress();
email.setPerson("user"+i);
email.setAdress("user"+i+"@163.com");
emailList.add(email);
}
adressList.setEmailList(emailList);
String obj_xml=xstream.toXML(adressList);
System.out.println(obj_xml);
<adressList>
<username>chb</username>
<emailList>
<email>
<person>user1</person>
<adress>user1@163.com</adress>
</email>
<email>
<person>user2</person>
<adress>user2@163.com</adress>
</email>
<email>
<person>user3</person>
<adress>user3@163.com</adress>
</email>
</emailList>
</adressList>
<username>chb</username>
<emailList>
<email>
<person>user1</person>
<adress>user1@163.com</adress>
</email>
<email>
<person>user2</person>
<adress>user2@163.com</adress>
</email>
<email>
<person>user3</person>
<adress>user3@163.com</adress>
</email>
</emailList>
</adressList>