用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
Posted on 2007-06-05 19:06 Stephen Wong 阅读(1224) 评论(0) 编辑 收藏 网摘 所属分类: 数据持久化、序列化 , WebService , Java![](https://i-blog.csdnimg.cn/blog_migrate/99891d7b6eb2b82cfbe642d3de70b290.jpeg)
XStream 项目主页 :
http://xstream.codehaus.org/
需要 lib :
xstream-1.2.2.jar
xpp3_min-1.1.3.4.O.jar
这是 2007.5.24 打包的 lib
测试主程序 :
package
org.stephencat.test;
import com.thoughtworks.xstream. * ;
import java.util. * ;
/**
* 测试 XStream 序列化和反序列化
* @author stephen.wjj
*
*/
public class XStreamTest {
/**
* 测试序列化
*/
public static void TestSerializer(){
System.out.println( " ===== Testing serializer ===== " );
Person p1 = new Person();
p1.setName( " AAA " );
Person p2 = new Person();
p2.setName( " BBB " );
Person p3 = new Person();
p3.setName( " CCC " );
Group g = new Group();
g.getPersons().add(p1);
g.getPersons().add(p2);
g.getPersons().add(p3);
g.setName( " KFC " );
XStream xstream = new XStream();
// 声明标记名称对应的 Java 类
xstream.alias( " group " , Group. class );
xstream.alias( " person " , Person. class );
// 把 Group 的 name 属性视为 XML 属性
xstream.useAttributeFor(Group. class , " name " );
// 忽略 XML 中的 <persons></persons> 标记
xstream.addImplicitCollection(Group. class , " persons " );
System.out.println(xstream.toXML(g));
}
/**
* 测试反序列化
*/
public static void TestDeserializer(){
System.out.println( " ===== Testing deserializer ===== " );
String xml = " <group name=/ " KFC/ " ><person><name>AAA</name></person><person><name>BBB</name></person><person><name>CCC</name></person></group> " ;
XStream xstream = new XStream();
// 声明标记名称对应的 Java 类
xstream.alias( " group " , Group. class );
xstream.alias( " person " , Person. class );
// 把 Group 的 name 属性视为 XML 属性
xstream.useAttributeFor(Group. class , " name " );
// 忽略 XML 中的 <persons></persons> 标记
xstream.addImplicitCollection(Group. class , " persons " );
Group g = (Group) xstream.fromXML(xml);
System.out.println( " Group name = " + g.getName());
System.out.println( " Group has persons = " + g.getPersons().size());
System.out.println( " Group persons: " );
if (g.getPersons().size() > 0 ){
Iterator it = g.getPersons().iterator();
while (it.hasNext()){
Person p = (Person)it.next();
if (p != null ){
System.out.println(p.getName());
} else {
System.out.println( " Err! person instance is null! " );
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestSerializer();
TestDeserializer();
}
}
import com.thoughtworks.xstream. * ;
import java.util. * ;
/**
* 测试 XStream 序列化和反序列化
* @author stephen.wjj
*
*/
public class XStreamTest {
/**
* 测试序列化
*/
public static void TestSerializer(){
System.out.println( " ===== Testing serializer ===== " );
Person p1 = new Person();
p1.setName( " AAA " );
Person p2 = new Person();
p2.setName( " BBB " );
Person p3 = new Person();
p3.setName( " CCC " );
Group g = new Group();
g.getPersons().add(p1);
g.getPersons().add(p2);
g.getPersons().add(p3);
g.setName( " KFC " );
XStream xstream = new XStream();
// 声明标记名称对应的 Java 类
xstream.alias( " group " , Group. class );
xstream.alias( " person " , Person. class );
// 把 Group 的 name 属性视为 XML 属性
xstream.useAttributeFor(Group. class , " name " );
// 忽略 XML 中的 <persons></persons> 标记
xstream.addImplicitCollection(Group. class , " persons " );
System.out.println(xstream.toXML(g));
}
/**
* 测试反序列化
*/
public static void TestDeserializer(){
System.out.println( " ===== Testing deserializer ===== " );
String xml = " <group name=/ " KFC/ " ><person><name>AAA</name></person><person><name>BBB</name></person><person><name>CCC</name></person></group> " ;
XStream xstream = new XStream();
// 声明标记名称对应的 Java 类
xstream.alias( " group " , Group. class );
xstream.alias( " person " , Person. class );
// 把 Group 的 name 属性视为 XML 属性
xstream.useAttributeFor(Group. class , " name " );
// 忽略 XML 中的 <persons></persons> 标记
xstream.addImplicitCollection(Group. class , " persons " );
Group g = (Group) xstream.fromXML(xml);
System.out.println( " Group name = " + g.getName());
System.out.println( " Group has persons = " + g.getPersons().size());
System.out.println( " Group persons: " );
if (g.getPersons().size() > 0 ){
Iterator it = g.getPersons().iterator();
while (it.hasNext()){
Person p = (Person)it.next();
if (p != null ){
System.out.println(p.getName());
} else {
System.out.println( " Err! person instance is null! " );
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestSerializer();
TestDeserializer();
}
}
Console 输出 :
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
注:
用于反序列化的字符串(String xml = "...")中带有 XML 头部时不会影响结果
<?
xml version="1.0"
?>
用于序列化的实体类 :
Group
package
org.stephencat.test;
import java.util. * ;
/**
* 包含若干 Person 的 Group
* @author stephen
*
*/
public class Group {
/**
* Group名称<br>
* 需求规定这是 XML 中的一个属性
*/
private String name;
/**
* 需求规定这是一堆若干数量的 person 标记
*/
private List persons = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public List getPersons() {
return persons;
}
public void setPersons(List persons) {
this .persons = persons;
}
}
import java.util. * ;
/**
* 包含若干 Person 的 Group
* @author stephen
*
*/
public class Group {
/**
* Group名称<br>
* 需求规定这是 XML 中的一个属性
*/
private String name;
/**
* 需求规定这是一堆若干数量的 person 标记
*/
private List persons = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public List getPersons() {
return persons;
}
public void setPersons(List persons) {
this .persons = persons;
}
}
Person
package org.stephencat.test;
/**
* Group 里面的 Person
* @author stephen
*
*/
public class Person {
/**
* 需求规定这是一个 name 标记
*/
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
}