java 序列化反序列化xml【使用XStream类库】
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 = new XStream(); 这种方式的初始化需要xpp3-[version].jar的支持。xpp是一个快速解析XML文件的解析器。
· XStream xstream = new XStream(new DomDriver()); XStream xStream = new XStream(new DomDriver("utf-8")) 这种方式不需要 XPP3.jar的支持,它是使用标准的JAXP DOM来解析它
应:ejbtimer的要求增加了数组对象的序列化测试,不过我测试时没有设置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
-
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("test@test.com");
-
-
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("contact1@test.com");
-
-
ContactBean contact2 = new ContactBean();
-
contact2.setPostCode("20002");
-
contact2.setTelephone("021-22222222");
-
contact2.setMobile("13922222222");
-
contact2.setEmail("contact2@test.com");
-
-
ContactBean[] contactArr = new ContactBean[] { contact1, contact2 };
-
userBean.setContactArr(contactArr);
-
return userBean;
-
}
-
-
}
-
-
-
测试UserBean:UserBean.java
-
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;
-
}
-
-
}
-
-
-
测试ContactBean :ContactBean.java
-
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;
-
}
-
}
-
-
-
运行测试代码,打印结果如下:
-
引用
-
-
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
-
-
生成的XML文件userbean.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>
-
-
-
到此测试例子结束,是不是觉得很方便啊,吼吼
文章来源: bugstack.blog.csdn.net,作者:Yao__Shun__Yu,版权归原作者所有,如需转载,请联系作者。
原文链接:bugstack.blog.csdn.net/article/details/17397891
- 点赞
- 收藏
- 关注作者
评论(0)