Overview
The most recently compiled Java aspects of XML serialization and deserialization of common tool classes, found dom4j and XStream. Dom4j relatively small, very good interpretation of XML, but for the object of XML serialization and deserialization, I still prefer xsteam (PS: Personal hobby bar), here to organize xstream basic knowledge of the entry;
Use
Referencing MAVEN content
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactid>xstream </artifactId> <version>1.4. 8</version> </dependency>
How aliases are configured
1, can be implemented by class annotations @xstreamalias, such as Xstreamalias ("book");
In this way, you need to pay attention to xStream instantiation, plus xstream.processannotations (bookstore.class); By way of annotations, there must be this sentence
2, can be configured by the XStream settings, such as Xstream.alias ("book", Book.class);
Other precautions
1, the property of a class, as the attribute of the XML header information, rather than the child nodes, need to use useattributefor, such as: Xstream.useattributefor (Book.class, "id");
2, hide with the attribute node, need to use addimplicitcollection, such as: 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 classBookstore {PrivateList<book>Books; PublicBookstore (list<book>Books) { This. Books =Books; } PublicList<book>Getbooks () {returnBooks; } Public voidSetbooks (list<book>Books) { This. Books =Books; }}
Book Class
Package Com.dbgo.xmldemo;import Com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias (" Book") Public classBook {PrivateInteger ID; PrivateString name; PrivateString author; PrivateString Year; Private DoublePrice ; PublicBook (Integer ID, string name, string author, string year,DoublePrice ) { This. ID =ID; This. Name =name; This. Author =author; This. Year =Year ; This. Price =Price ; } .... Omit Get/set property ....}
Using Class annotation parsing methods
protected Static voidXstreamdemo (String xmsource) {XStream XStream=NewXStream (); Xstream.addimplicitcollection (bookstore.class,"Books"); Xstream.processannotations (bookstore.class);//by way of annotations, there must be this sentenceXstream.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 annotation methods
protected Static voidXstreamdemo (String xmsource) {XStream XStream=NewXStream (); 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=NewBookstore (NewArraylist<>()); Bookstore.getbooks (). Add (NewBook1,"Zhang","Hi"," at", the)); 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> Song of Ice and Fire </name>"+"<author> George Martin </author>"+"<year>2014</year>"+"<price>89</price>"+"</book>"+"<book id=\ "2\" >"+"<name> Andersen fairy tale </name>"+"<year>2004</year>"+"<price>77</price>"+"<author>English</author>"+"</book>"+"</bookstore>";
Reference Blog
Java generates two other ways to parse XML XStream https://www.cnblogs.com/happyPawpaw/p/4972650.html
Java+xstream implementation of XML serialization 54893582
XStream implementing serialization and deserialization of objects (Java)