Prepare two beans first
1 public class book {2 Private int bookid; 3 private string bookname; 4 private string bookcode; 5 6... (The get and set methods are omitted) 7}
Public class bookshelf {private int number; private list <book> books; private string remark; private date; Public bookshelf () {books = new arraylist <book> ();}... (The get and set methods are omitted )}
Use
1 book book1 = New Book (); 2 book1.setbookcode ("001"); 3 book1.setbookid (1); 4 book1.setbookname ("Java programming "); 5 book book2 = New Book (); 6 book2.setbookcode ("002"); 7 book2.setbookid (2); 8 book2.setbookname ("JSP programming "); 9 bookshelf = new bookshelf (); 10 bookshelf. setnumber (1); 11 bookshelf. setremark ("first bookshelf"); 12 bookshelf. getbooks (). add (book1); 13 bookshelf. getbooks (). add (book2); 14 bookshelf. setdate (new date (); 15 16 xstream = new xstream (New domdriver (); 17 string STR = xstream. toxml (bookshelf); 18 system. out. println (STR );
Output result:
<XML. bookshelf> <number> 1 </number> <books> <XML. book> <bookid> 1 </bookid> <bookname> JAVA Programming </bookname> <bookcode> 001 </bookcode> </XML. book> <XML. book> <bookid> 2 </bookid> <bookname> JSP programming </bookname> <bookcode> 002 </bookcode> </XML. book> </books> <remark> first bookshelf </remark> <date> 07:03:34. 485 UTC </date> </XML. bookshelf>
In the output result, the class name is converted to the node name, but the preceding XML (uncomfortable) is added, and the time format string is also uncomfortable. Let's modify it!
1. First, let's take a look at the time format issue. We need to define how to convert the date field. Of course, xstream provides an interface for us to implement it.
Public class muconverter implements converter {// determine whether the field belongs to the type to be converted @ override public Boolean canconvert (class paramclass) {return date. class. isassignablefrom (paramclass);} // convert the object to XML @ override public void Marshal (Object object, hierarchicalstreamwriter writer, marshallingcontext context) {simpledateformat format = new simpledateformat ("yyyy-mm-dd"); writer. setvalue (format. format (object);} // convert XML to @ override public object unmarshal (hierarchicalstreamreader reader, unmarshallingcontext context) {try {date = dateformat. getinstance (). parse (reader. getvalue (); return date;} catch (parseexception e) {return NULL ;}}}
2. Change the name of each field
Xstream. alias ("bookshelf", bookshelf. class); xstream. alias ("book", book. class); // modify the node name xstream. aliasfield ("other", bookshelf. class, "remark"); // The field is not a node, but an attribute xstream. aliasattribute (book. class, "bookid", "ID"); xstream. useattributefor (book. class, "bookid"); // remove the parent node of the group node // xstream. addimplicitcollection (bookshelf. class, "books"); // custom converter xstream. registerconverter (New muconverter ());*/
Output result
<Bookshelf> <number> 1 </number> <books> <book id = "1"> <bookname> JAVA Programming </bookname> <bookcode> 001 </bookcode> </book> <book id = "2"> <bookname> JSP programming </bookname> <bookcode> 002 </bookcode> </book> </books> <other> first bookshelves </other> <date> 2014-07-04 </date> </bookshelf>
3. If you think the specified code above is too complicated, you can use annotations to implement it.
First, let's look at two beans.
@XStreamAlias("Book")public class Book { @XStreamAlias("id") @XStreamAsAttribute private int bookId; private String bookName; private String bookCode; ... }
@ Xstreamalias ("bookshelf") public class bookshelf {private int number; // remove the parent node of the group node // @ xstreamimplicit private list <book> books; private string remark; @ xstreamconverter (muconverter. class) private date; Public bookshelf () {books = new arraylist <book> ();}...}
Fields without annotations are converted by field name by default.
Call the method directly.
Xstream. autodetectannotations (true); // you can also call // xstream. processannotations (bookshelf. Class); // xstream. processannotations (book. Class );
The above section describes how to convert an object to XML. to convert an XML object to an object, it is very simple (Note: to convert an XML object, you must specify the conversion using the alias or annotation above, the following code cannot be called directly)
XStream xStream=new XStream(new DomDriver());xStream.autodetectAnnotations(true);BookShelf bookShelf2=(BookShelf)xStream.fromXML(str);
Support for serialization and deserialization
Not to mention, directly add the code
1. deserialization
ObjectInputStream input=xStream.createObjectInputStream(inputStream);BookShelf bookShelf=(BookShelf)input.readObject();
2. serialization
ObjectOutputStream out=xstream.createObjectOutputStream(outputStream);out.writeObject(bookShelf);out.close();
3. prettyprintwriter and compactwriter
These two are XML files output by xstream. The difference is that the XML output by the latter is connected, and the former outputs formatted XML files.