zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    等待队列设备[置顶] Linux设备驱动,等待队列
    宠物功能[置顶] QQ宠物保姆
    选中拖动Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(下)
    序列化对象java中为什么要实现序列化,什么时候实现序列化?
    函数表达式[置顶] 母函数详解
    文件问题cocos2dx&cocosbuilder折腾记
    模块functionJavaScript学习笔记(二十五) 沙箱模式
    nullnullflume ng配置拓扑图
    对象序列化对象的序列化和反序列化
    扩展编程PHP自学之路PHP数据库编程
  • 原文地址:https://www.cnblogs.com/xibei666/p/8998998.html
Copyright © 2011-2022 走看看