xstream implements object serialization and deserialization (Java)

Overview

  The latest sorting out the common tool classes for XML serialization and deserialization in Java, and found dom4j and xstream. dom4j is relatively small and can interpret xml very well; but for the xml serialization and deserialization of objects, I still prefer xsteam (ps: personal preference), here is the basic knowledge of xstream;

use

Quoting maven content

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

How to configure aliases

1. @XStreamAlias ​​can be implemented through class annotations, such as XStreamAlias("book");

When using this method, you need to pay attention to when xStream is instantiated, add xStream.processAnnotations(Bookstore.class); //By way of annotation, you must have this sentence

2. It can be achieved by configuring Xstream settings; such as xStream.alias("book",book.class);

Other considerations

1. Use the attribute of a certain class as the attribute of the xml header information instead of the child node, you need to use useAttributeFor, for example: xStream.useAttributeFor(book.class,"id");

2. When hiding and attribute nodes, you need to use addImplicitCollection, for example: xStream.addImplicitCollection(Bookstore.class,"books");

The complete code is as follows:

Bookstore class:

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 class

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;
    }

    ....omit get / set properties....
}

Resolving methods using class annotations

   protected static void XstreamDemo(String xmsource)
    {
        XStream xStream=new XStream();
        xStream.addImplicitCollection(Bookstore.class,"books");
        xStream.processAnnotations(Bookstore.class ); // By way of annotation, there must be this sentence xStream.processAnnotations 
        (book.class ) ;
        Object bookstore2= xStream.fromXML(xmsource);

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

    }

Using non-class annotated methods

   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);

    }

The xml message is as follows

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>";

Reference blog

Java generates two other methods of parsing xml Xstream  https://www.cnblogs.com/happyPawpaw/p/4972650.html

java+xstream implements xml serialization   https://blog.csdn.net/jaryle/article/details/54893582

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325482580&siteId=291194637