概述
最新整理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
原文地址:https://www.cnblogs.com/xibei666/p/8998998.html