XStream使用详解
1.Xstream介绍
class Person//JavaBean实体类
{
private String name;
private int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream();
//XML序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//XML反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
//Json序列化
String json=xstream.toXML(bean);
System.out.println(json);
//Json反序列
bean=(Person)xstream.fromXML(json);
System.out.println(bean);
}
}
<test.Person>
<name>张三</name>
<age>19</age>
</test.Person>
Person [name=张三, age=19]
{"test.Person":{"name":"张三","age":19}}
Person [name=张三, age=19
2.Xstream的基本使用
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
//XStream xstream = new XStream();//需要XPP3库
//XStream xstream = new XStream(new DomDriver());//不需要XPP3库
XStream xstream = new XStream(new StaxDriver());//不需要XPP3库开始使用Java6
xstream.alias("人",Person.class);//为类名节点重命名
//XML序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//XML反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<?xml version="1.0" ?><人><name>张三</name><age>19</age></人>
Person [name=张三, age=19]
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream(new JettisonMappedXmlDriver());//设置Json解析器
xstream.setMode(XStream.NO_REFERENCES);//设置reference模型,不引用
xstream.alias("人",Person.class);//为类名节点重命名
//Json序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//Json反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
{"人":{"name":"张三","age":19}}
Person [name=张三, age=19]
3.Xstream序列化重命名
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream();
xstream.aliasPackage("com.lzw", "test");//为包名称重命名
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<com.lzw.Person>
<name>张三</name>
<age>19</age>
</com.lzw.Person>
Person [name=张三, age=19]
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream();
xstream.alias("人", Person.class);//为类名节点重命名
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<人>
<name>张三</name>
<age>19</age>
</人>
Person [name=张三, age=19]
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream();
xstream.aliasField("姓名", Person.class,"name");//为类的字段节点重命名
xstream.aliasField("年龄", Person.class,"age");//为类的字段节点重命名
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<test.Person>
<姓名>张三</姓名>
<年龄>19</年龄>
</test.Person>
Person [name=张三, age=19]
class Person
{
private String name;
private int age;
private List friends;
public Person(String name, int age, String... friends)
{
this.name = name;
this.age = age;
this.friends = Arrays.asList(friends);
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
}
}
public class Test
{
public static void main(String[] args)
{
Person bean =new Person("张三",19,"李四","王五","赵六");
XStream xstream = new XStream();
xstream.addImplicitCollection(Person.class, "friends");//省略集合根节点
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<test.Person>
<name>张三</name>
<age>19</age>
<string>李四</string>
<string>王五</string>
<string>赵六</string>
</test.Person>
Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
public class Test
{
public static void main(String[] args)
{
Person bean =new Person("张三",19,"李四","王五","赵六");
XStream xstream = new XStream();
xstream.useAttributeFor(Person.class, "name");//把字段节点设置成属性
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<test.Person name="张三">
<age>19</age>
<friends class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>李四</string>
<string>王五</string>
<string>赵六</string>
</a>
</friends>
</test.Person>
Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
public class Test
{
public static void main(String[] args)
{
Person bean =new Person("张三",19,"李四","王五","赵六");
XStream xstream = new XStream();
xstream.omitField(Person.class, "friends");//把字段节点隐藏
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<test.Person>
<name>张三</name>
<age>19</age>
</test.Person>
Person [name=张三, age=19, friends=null]
4.Xstream注解的使用
XStream xstream = new XStream();
xstream.processAnnotations(Person.class);//应用Person类的注解
xstream.autodetectAnnotations(true);//自动检测注解
@XStreamAlias("人")
class Person
{
@XStreamAlias("姓名")
private String name;
@XStreamAlias("年龄")
private int age;
@XStreamAlias("朋友")
private List friends;
public Person(String name, int age, String... friends)
{
this.name = name;
this.age = age;
this.friends = Arrays.asList(friends);
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
}
}
<人>
<姓名>张三</姓名>
<年龄>19</年龄>
<朋友 class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>李四</string>
<string>王五</string>
<string>赵六</string>
</a>
</朋友>
</人>
Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
class Person
{
private String name;
private int age;
//@XStreamImplicit//只隐藏集合根节点
@XStreamImplicit(itemFieldName="朋友")//设置重复的节点名,可能会导致无法反序列化
private List<String> friends;
public Person(String name, int age, String... friends)
{
this.name = name;
this.age = age;
this.friends = Arrays.asList(friends);
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
}
}
<test.Person>
<name>张三</name>
<age>19</age>
<朋友>李四</朋友>
<朋友>王五</朋友>
<朋友>赵六</朋友>
</test.Person>
Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
class Person
{
@XStreamAsAttribute
private String name;
@XStreamAsAttribute
private int age;
private List<String> friends;
public Person(String name, int age, String... friends)
{
this.name = name;
this.age = age;
this.friends = Arrays.asList(friends);
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
}
}
<test.Person name="张三" age="19">
<friends class="java.util.Arrays$ArrayList">
<a class="string-array">
<string>李四</string>
<string>王五</string>
<string>赵六</string>
</a>
</friends>
</test.Person>
Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
class Person
{
private String name;
private int age;
@XStreamOmitField
private List<String> friends;
public Person(String name, int age, String... friends)
{
this.name = name;
this.age = age;
this.friends = Arrays.asList(friends);
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]";
}
}
<test.Person>
<name>张三</name>
<age>19</age>
</test.Person>
Person [name=张三, age=19, friends=null]
class Person
{
private String name;
private int age;
@XStreamConverter(value=BooleanConverter.class,booleans={false},strings={"男","女"})
private boolean sex;
public Person(String name, int age, boolean sex)
{
this.name = name;
this.age = age;
this.sex=sex;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
<test.Person>
<name>张三</name>
<age>19</age>
<sex>男</sex>
</test.Person>
Person [name=张三, age=19, sex=true]
5.Xstream自定义的转换器
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class PersonConverter implements Converter
{
@Override//定义转换器能转换的JavaBean类型
public boolean canConvert(Class type)
{
return type.equals(Person.class);
}
@Override//把对象序列化成XML或Json
public void marshal(Object value, HierarchicalStreamWriter writer,
MarshallingContext context)
{
Person person = (Person) value;
writer.startNode("姓名");
writer.setValue(person.getName());
writer.endNode();
writer.startNode("年龄");
writer.setValue(person.getAge()+"");
writer.endNode();
writer.startNode("转换器");
writer.setValue("自定义的转换器");
writer.endNode();
}
@Override//把XML或Json反序列化成对象
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context)
{
Person person = new Person("",-1);
reader.moveDown();
person.setName(reader.getValue());
reader.moveUp();
reader.moveDown();
person.setAge(Integer.parseInt(reader.getValue()));
reader.moveUp();
return person;
}
}
public class Test
{
public static void main(String[] args)
{
Person bean =new Person("张三",19);
XStream xstream = new XStream();
xstream.registerConverter(new PersonConverter());//注册转换器
//序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
<test.Person>
<姓名>张三</姓名>
<年龄>19</年龄>
<转换器>自定义的转换器</转换器>
</test.Person>
Person [name=张三, age=19]
6.Xstream对象流的使用
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
}
public class Test
{
public static void main(String[] args) throws IOException
{
XStream xstream = new XStream();
ObjectOutputStream out = xstream.createObjectOutputStream(System.out);
out.writeObject(new Person("张三",12));
out.writeObject(new Person("李四",19));
out.writeObject("Hello");
out.writeInt(12345);
out.close();
}
}
<object-stream>
<test.Person>
<name>张三</name>
<age>12</age>
</test.Person>
<test.Person>
<name>李四</name>
<age>19</age>
</test.Person>
<string>Hello</string>
<int>12345</int>
</object-stream>
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class Test
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
String s="<object-stream><test.Person><name>张三</name><age>12</age></test.Person><int>12345</int></object-stream>";
StringReader reader = new StringReader(s);
XStream xstream = new XStream();
ObjectInputStream in = xstream.createObjectInputStream(reader);
System.out.println((Person) in.readObject());
System.out.println(in.readInt());
}
}
Person [name=张三, age=12]
12345
7.Xstream持久化API
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class Test
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp"));
List list = new XmlArrayList(strategy);
list.add(new Person("张三",13));//保存数据
list.add(new Person("李四",21));
list.add(new Person("王五",17));
}
}
public class Test
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp"));
List list = new XmlArrayList(strategy);
for (Iterator it = list.iterator(); it.hasNext();)
{
System.out.println((Person) it.next());
it.remove();//删除对象序列化文件
}
}
}
Person [name=张三, age=13]
Person [name=李四, age=21]
Person [name=王五, age=17]
8.Xstream操作Json
@XStreamAlias("人")
class Person
{
@XStreamAlias("姓名")
private String name;
@XStreamAlias("年龄")
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class Test
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream(new JettisonMappedXmlDriver());//设置Json解析器
xstream.autodetectAnnotations(true);
//Json序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
//Json反序列化
bean=(Person)xstream.fromXML(xml);
System.out.println(bean);
}
}
{"人":{"姓名":"张三","年龄":19}}
Person [name=张三, age=19]
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class Test00
{
public static void main(String[] args)
{
Person bean=new Person("张三",19);
XStream xstream = new XStream(new JsonHierarchicalStreamDriver()
{
public HierarchicalStreamWriter createWriter(Writer writer)
{
return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
}
});
//Json序列化
String xml = xstream.toXML(bean);
System.out.println(xml);
}
}
{
"name": "张三", "age": 19 }
- JettisonMappedXmlDriver:是支持序列化和反序列化Json的。
- JsonHierarchicalStreamDriver:只支持序列化,不支持反序列化。
XStream使用详解的更多相关文章
- Spring MVC测试框架详解——服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- SpringMvc测试框架详解----服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- Spring学习(一)-----Spring 模块详解
官方下载链接:http://repo.spring.io/release/org/springframework/spring/ Spring 模块详解: Core 模块 spring-beans-3 ...
- struts-2.3.24.1中的jar的详解
Struts2.3.24.1核心Jar包详解 antlr-2.7.2.jar 语言转换工具,它是接受词文法语言描述,并能产生识别这些语言的语句的程序的一种工具 a ...
- maven打包插件详解
maven-jar-plugin插件的使用及详解 该插件的xml配置及详解如下: <plugin> <groupId>org.apache.maven.plugins</ ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
随机推荐
- C#关于一个程序,只可以有一种实例的方法
方法一:使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.把program.cs文件里的Main()函数改为如下代码: 说明:程序中通过语句 System.Threading.Mutex ru ...
- scala学习笔记1
一.REPL scala解释器读到一个表达式,对它进行求值,将它的打印出来,接着再继续读下一个表达式.这个过程被称作 读取-打印-循环,即REPL. 从技术上讲,scala程序并不是一个解释器.实际发 ...
- 自制tab页
没事自己弄着玩,写了个tab页.不要当真.想看就看看.希望相互学习. 效果预览:html源码: <!DOCTYPE html> <html lang="en"&g ...
- WIN7 shutdown 定时/倒计时 命令关机
解决方案: 一.可以通过DOS命令shutdown来解决 在 Win7 中,shutdown实现自动关机的方法如下: 开始->运行->cmd 运行"shutdown -s -t ...
- How to create Lookup Worker Filtered by Legal Entity[AX2012]
1. Add a new method in hcmWorker table, and add this script : public static client void lookupWorker ...
- angularjs2 学习笔记(二) 组件
angular2 组件 首先了解angular2 组件的含义 angular2的应用就是一系列组件的集合 我们需要创建可复用的组件供多个组件重复使用 组件是嵌套的,实际应用中组件是相互嵌套使用的 组件 ...
- 《Prism 5.0源码走读》Bootstrapper
Prism框架需要在应用程序启动的时候进行一些初始化的工作,Bootstrapper就是来做这些的,是其切入点. Bootstrapper主要要做的事有:创建和配置module catalog,创建D ...
- equals函数
equals函数在Object类当中,而Object类是所有类的父类,所以所有的类里面都有equals函数. “==”操作符之前用于比较两个基本数据类型的值是否相等,而对于引用数据类型,“==”操作符 ...
- 使用Linux调用资源库中的Job报错-ERROR: No repository provided, can't load job.
使用kettle调用资源库中的作业或者是转换,需要注意一下两个问题: 问题一:(-rep后不需要IP)标准shell代码如下 #!/bin/bash export JAVA_HOME=/usr/lib ...
- 如何从官网下载springframework和document
spring官网 http://spring.io/ --->spring project--->点击github图标 --->artifactory --->进入到了http ...