码迷,mamicode.com
首页 > 编程语言 > 详细

xstream实现对象的序列化和反序列化(Java)

时间:2018-05-06 18:53:05      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:.com   logs   xml序列化   this   AC   生成   RoCE   depend   stream   

概述

  最新整理Java方面XML序列化和反序列化的常用工具类,找到了dom4j和xstream。dom4j相对小巧,很好的解读xml;但是对于对象的xml序列化和反序列化,我还是比较喜欢xsteam(ps:个人爱好吧),这里整理xstream的入门基础知识;

使用

引用maven内容

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

别名配置的方式

1、可以通过类注解实现@XStreamAlias,比如XStreamAlias("book");

采用这种方式的时候,需要注意xStream实例化的时候,加上 xStream.processAnnotations(Bookstore.class); //通过注解方式的,一定要有这句话

2、可以通过配置Xstream设置实现;比如xStream.alias("book",book.class);

其他注意事项

1、将某一个类的属性,作为xml头信息的属性,而不是子节点  ,需要使用useAttributeFor,比如:xStream.useAttributeFor(book.class,"id");

2、隐藏跟属性节点的时候,需要使用addImplicitCollection,比如:xStream.addImplicitCollection(Bookstore.class,"books");

完整代码如下:

Bookstore类:

package com.dbgo.xmldemo;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.util.List;
@XStreamAlias("bookstore")
public class Bookstore {
    private List<book> books;

    public Bookstore(List<book> books) {
        this.books = books;
    }
    public List<book> getBooks() {
        return books;
    }
    public void setBooks(List<book> books) {
        this.books = books;
    }
}

book类

package com.dbgo.xmldemo;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("book")
public class book {
    private Integer id;
    private String name;
    private String author;
    private String year;
    private double price;

    public book(Integer id, String name, String author, String year, double price) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.year = year;
        this.price = price;
    }

    ....省略get/set属性....
}

使用类注解解析方法

   protected static void XstreamDemo(String xmsource)
    {
        XStream xStream=new XStream();
        xStream.addImplicitCollection(Bookstore.class,"books");
        xStream.processAnnotations(Bookstore.class); //通过注解方式的,一定要有这句话
        xStream.processAnnotations(book.class);
        Object bookstore2= xStream.fromXML(xmsource);

        String xmocontent=xStream.toXML(bookstore2);
        System.out.println(xmocontent);
        //Bookstore bookstore1= (Bookstore)xStream.fromXML(xmocontent);

    }

使用非类注解方法

   protected static void XstreamDemo(String xmsource)
    {
        XStream xStream=new XStream();
        xStream.alias("book",book.class);
        xStream.alias("bookstore",Bookstore.class);
        xStream.useAttributeFor(book.class,"id");
        xStream.addImplicitCollection(Bookstore.class,"books");       
        Object bookstore2= xStream.fromXML(xmsource);
        Bookstore bookstore=new Bookstore(new ArrayList<>());
        bookstore.getBooks().add(new book(1,"zhang","hi","23",34));
        String xmocontent=xStream.toXML(bookstore);
        System.out.println(xmocontent);
        //Bookstore bookstore1= (Bookstore)xStream.fromXML(xmocontent);

    }

xml报文如下

String xmlSource="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<bookstore>" +
                "    <book id=\"1\">" +
                "        <name>冰与火之歌</name>" +
                "        <author>乔治马丁</author>" +
                "        <year>2014</year>" +
                "        <price>89</price>" +
                "    </book>" +
                "    <book id=\"2\">" +
                "        <name>安徒生童话</name>" +
                "        <year>2004</year>" +
                "        <price>77</price>" +
                "        <author>English</author>" +
                "    </book>    " +
                "</bookstore>";

参考博客

java生成解析xml的另外两种方法Xstream  https://www.cnblogs.com/happyPawpaw/p/4972650.html

java+xstream实现xml序列化  https://blog.csdn.net/jaryle/article/details/54893582

xstream实现对象的序列化和反序列化(Java)

标签:.com   logs   xml序列化   this   AC   生成   RoCE   depend   stream   

原文地址:https://www.cnblogs.com/xibei666/p/8998998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!