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


    概述

      最新整理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");

    3、在Spring Boot下使用,XML转对象时,同一个类无法进行转换,原因是因为SpringBoot重新加载了对象;若未指定classloader的时候,SpringBoot未正确使用classloader,需要指定classloader,需要在方法中指定加载的类,添加如下代码: xstream.setClassLoader(clazz.getClassLoader());

    完整代码如下:

    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);
         xstream.setClassLoader(Bookstore.class.getClassLoader());//在spring boot一定加上这句话 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

    Bookstore.class
  • 相关阅读:
    [apue] FIFO:不是文件的文件
    [apue] 等待子进程的那些事儿
    [apue] popen/pclose 疑点解惑
    [apue] 使用 popen/pclose 的一点疑问
    [apue] 使用 poll 检测管道断开
    [apue] dup2的正确打开方式
    [apue] 管道原子写入量的一个疑问
    [apue] 测试管道容量的一些疑问
    【新阁教育】再也不用担心我的PLC通信不上了
    【新阁教育】三菱PLC的这个功能,真的很强大
  • 原文地址:https://www.cnblogs.com/xibei666/p/8998998.html
Copyright © 2020-2023  润新知