XStream使用

简介

官网地址 http://x-stream.github.io
官网教程地址 http://x-stream.github.io/alias-tutorial.html
XStream是一个简单的基于Java的类库,用来将Java对象序列化成XML(JSON) 或 反序列化为对象 (即:可以轻易的将Java对象和XML文档相互转换)。XStream在运行时使用Java反射机制对要进行序列化的对象树的结构进行探索,并不需要对对象作出修改。

下载及依赖引入

官网下载
http://x-stream.github.io/download.html

上面下载地址网页中会有maven 引入版本

<dependency>
  <groupId>com.thoughtworks.xstream</groupId>
  <artifactId>xstream</artifactId>
  <version>1.4.20</version>
</dependency>

使用

1、创建 XStream 对象

1XStream xStream = new XStream();
【注意】默认为Xpp3解析器,它是一个非常快速的 XML 拉取解析器实现。但是有依赖关系,
需要引入 xstream-[version].jar 和 xpp3-[version].jar xmlpull-[version].jar 在类路径中
或 
2XStream xstream = new XStream(new DomDriver());
【注意】使用标准的 JAXP DOM 解析器
或
3XStream xstream = new XStream(new StaxDriver());
【注意】从 Java 6 开始使用集成的 StAX 解析器,StaxDriver使用SAX解析器(可从Java6),一个快速的XML解析器。

2、java bean与xml 互转使用方法

序列化对象到 XML
// Object to XML Conversion
String xml = xstream.toXML(Person);

反序列化 XML 获得对象
// XML to Object Conversion
Person person = (Person) xstream.fromXML(xml);

3、别名序列化映射关系使用

  • 类别名
XStream.alias(String name, Class type)
xstream.alias("student", Student.class);

如果不使用类别名标签会显示为完整名称
使用前<com.org.Student> 使用后<student>
  • 类属性别名
XStream.aliasField(String alias, Class definedIn, String fieldName)
xstream.aliasField("name", Student.class, "studentName");

使用前<studentName>==使用后 <name>
  • 类属性混叠(将类属性作为标签的属性而不是子标签)
xstream.useAttributeFor(Student.class, "studentName");

使用前 <student><studentName>
使用后 <student studentName="xiaoxi">
  • 隐式集合(将使用的集合在 XML 无需显示集合节点,直接显示集合的子节点)数组或映射也可以声明为隐式
XStream.addImplicitCollection(Class ownerType, String fieldName)
xstream.addImplicitCollection(Student.class, "noteList");

使用前<noteList><note></note><note></note></noteList>显示了noteList节点
使用后<note></note><note></note>,不显示noteList节点
用于解析没有根节点转换到list,同时将list在转换到没有根节点的多组节点
  • 包别名
xstream.aliasPackage("com.Student", "com.org.Student");

使用后<com.org.Student>使用后<com.Student>
  • 转换器( 属性不能直接转换的,还可以使用转换器
    参考转换器使用)
    http://x-stream.github.io/converter-tutorial.html

序列化与反序列化中映射关系==使用方式

  • 类别名方式
    // 创建一个类的XML完全限定名称的别名
    xstream.alias("student", Student.class);
    xstream.alias("note", Note.class);

    // 使用的集合是表示在XML无需显示根
    xstream.addImplicitCollection(Student.class, "notes");

    // 成员变量作为XML属性
    xstream.useAttributeFor(Student.class, "studentName");
    // 用于创建以XML字段的别名
    xstream.aliasField("name", Student.class, "studentName");
  • 注解别名方式
@XStreamAlias("student")    //类别名
class Student {    
    @XStreamAlias("name")   //字段别名
    @XStreamAsAttribute     //同时设置字段为xml标签属性<student name="xiaoxi">,没有@XStreamAsAttribute就是xml的子标签
    private String studentName;
    
    @XStreamImplicit        //define list as an implicit collection
    private List<Note> notes = new ArrayList<Note>();

【关键】

为了告诉 XStream 框架来处理注释,需要XML序列化之前添加下面的命令。
 xstream.processAnnotations(Student.class);
 或者
 xstream.autodetectAnnotations(true);
 
XStream xstream = new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
反序列化 XML 获得对象
Person person = (Person) xstream.fromXML(xml);
序列化对象到 XML
String xml = xstream.toXML(Person);
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值