Reprint Source: http://hwy584624785.iteye.com/blog/1168680
This example uses XStream to generate an XML file and then to serialize the contents of the XML file.
XStream is a simple class library that can serialize objects to XML, and can also restore XML to an object.
XStream Official website: http://xstream.codehaus.org/
The attachment provides XStream and XPP3 related jar downloads:
Xstream-1.2.2.jar
Xpp3-1.1.3.3_min.jar
In order to use XStream, you need to initialize it, there are two ways to initialize it:
- XStream XStream = new XStream (); The initialization of this method requires Xpp3-[version].jar support. XPP is a parser that parses XML files quickly.
- XStream XStream = new XStream (new Domdriver ()); XStream XStream = new XStream (New Domdriver ("Utf-8") This approach does not need to rely on Xpp3.jar support, it is used to parse it using the standard JAXP dom.
Also here is a simple description of the mode parameter:
When XStream serializes or deserializes an object, it creates two classes Marshallingcontext and Unmarshallingcontext, which process the data and delegate the appropriate converters. XStream provides a three default implementation of the context, with subtle differences between them. The default value can be changed by Method Xstream.setmode (), and one of the following parameters needs to be passed:
- Xstream.xpath_relative_references: (the default) is used to identify duplicate references using XPATH and relative path representations.
- Xstream.xpath_absolute_references: An XPATH is used to identify duplicate references, using an absolute path representation.
- Xstream.id_references: Use ID citation to identify duplicate references. On some occasions (when writing XML), it will be easier to manipulate
- Xstream.no_references: This situation loses support for graphical objects, and only looks at the object as a tree structure. Repeated references are considered to be two different objects, and circular references can cause an exception to occur. This mode is faster compared to the above two modes, and memory consumption is more
This example contains 3 simple Java classes, of which two are JavaBean and the other is a logical processing class. Two classes of JavaBean are as follows: Person.javajava code
- Public class Person {
- private int id;
- private String name;
- private int age;
- private PhoneNumber phone;
- public int getId () {
- return ID;
- }
- public void setId (int id) {
- this.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- public int getage () {
- return age;
- }
- public void Setage (int.) {
- this.age = age;
- }
- Public PhoneNumber Getphone () {
- return phone;
- }
- public void Setphone (PhoneNumber phone) {
- this.phone = phone;
- }
- @Override
- Public String toString () {
- return "person [id=] + ID + ", name= "+ name + ", age= "+ Age
- + ", phone=" + Phone + "]";
- }
- }
Phonenumber.javajava Code
- Public class PhoneNumber {
- private int code;
- private String number;
- public int GetCode () {
- return code;
- }
- public void Setcode (int code) {
- This.code = code;
- }
- Public String GetNumber () {
- return number;
- }
- public void Setnumber (String number) {
- this.number = number;
- }
- @Override
- Public String toString () {
- return "PhoneNumber [code=" + code + ", number=" + number + "]";
- }
- }
The main implementation of the function is the following class: Xstreamtest.javajava code
- Import Java.io.File;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import Com.thoughtworks.xstream.XStream;
- Public class Xstreamtest {
- public static void Main (string[] args) throws Exception {
- Person person = Initperson ();
- XStream XStream = new XStream ();
- Xstream.alias (' person ', person. Class);
- FileOutputStream fileoutputstream=New FileOutputStream (new File ("Test.xml"));
- Xstream.toxml (Person,fileoutputstream);
- //Deserialization
- FileInputStream fileinputstream=New FileInputStream (new File ("Test.xml"));
- Person person1= (person) xstream.fromxml (FileInputStream);
- System.out.println (Person1.tostring ());
- }
- public static Person Initperson () {
- Person person = new Person ();
- Person.setid (20);
- Person.setname ("Huangwei");
- Person.setage (24);
- PhoneNumber PhoneNumber = new PhoneNumber ();
- Phonenumber.setcode (222);
- Phonenumber.setnumber ("15200000000");
- Person.setphone (PhoneNumber);
- return person;
- }
- }
The contents of the generated XML file are printed as follows: XML code
- <person>
- <ID>20</ID>
- <name>huangwei</name>
- <age>24</age>
- <phone>
- <code>222</code>
- <number>15200000000</number>
- </phone>
- </person>
The inverse sequence has been resolved as follows: Java code
- person [id=,Name=huangwei, age=, Phone=phonenumber [code=222, number=15200000000]
Reproduced -xstream Parsing of XML parsing