Reproduced -xstream Parsing of XML parsing

Source: Internet
Author: User

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
  1. Public class Person {
  2. private int id;
  3. private String name;
  4. private int age;
  5. private PhoneNumber phone;
  6. public int getId () {
  7. return ID;
  8. }
  9. public void setId (int id) {
  10. this.id = ID;
  11. }
  12. Public String GetName () {
  13. return name;
  14. }
  15. public void SetName (String name) {
  16. this.name = name;
  17. }
  18. public int getage () {
  19. return age;
  20. }
  21. public void Setage (int.) {
  22. this.age = age;
  23. }
  24. Public PhoneNumber Getphone () {
  25. return phone;
  26. }
  27. public void Setphone (PhoneNumber phone) {
  28. this.phone = phone;
  29. }
  30. @Override
  31. Public String toString () {
  32. return "person [id=] + ID + ", name= "+ name + ", age= "+ Age
  33. + ", phone=" + Phone + "]";
  34. }
  35. }
Phonenumber.javajava Code
  1. Public class PhoneNumber {
  2. private int code;
  3. private String number;
  4. public int GetCode () {
  5. return code;
  6. }
  7. public void Setcode (int code) {
  8. This.code = code;
  9. }
  10. Public String GetNumber () {
  11. return number;
  12. }
  13. public void Setnumber (String number) {
  14. this.number = number;
  15. }
  16. @Override
  17. Public String toString () {
  18. return "PhoneNumber [code=" + code + ", number=" + number + "]";
  19. }
  20. }
The main implementation of the function is the following class: Xstreamtest.javajava code
  1. Import Java.io.File;
  2. Import Java.io.FileInputStream;
  3. Import Java.io.FileOutputStream;
  4. Import Com.thoughtworks.xstream.XStream;
  5. Public class Xstreamtest {
  6. public static void Main (string[] args) throws Exception {
  7. Person person = Initperson ();
  8. XStream XStream = new XStream ();
  9. Xstream.alias (' person ', person.   Class);
  10. FileOutputStream fileoutputstream=New FileOutputStream (new File ("Test.xml"));
  11. Xstream.toxml (Person,fileoutputstream);
  12. //Deserialization
  13. FileInputStream fileinputstream=New FileInputStream (new File ("Test.xml"));
  14. Person person1= (person) xstream.fromxml (FileInputStream);
  15. System.out.println (Person1.tostring ());
  16. }
  17. public static Person Initperson () {
  18. Person person = new Person ();
  19. Person.setid (20);
  20. Person.setname ("Huangwei");
  21. Person.setage (24);
  22. PhoneNumber PhoneNumber = new PhoneNumber ();
  23. Phonenumber.setcode (222);
  24. Phonenumber.setnumber ("15200000000");
  25. Person.setphone (PhoneNumber);
  26. return person;
  27. }
  28. }
The contents of the generated XML file are printed as follows: XML code
  1. <person>
  2. <ID>20</ID>
  3. <name>huangwei</name>
  4. <age>24</age>
  5. <phone>
  6. <code>222</code>
  7. <number>15200000000</number>
  8. </phone>
  9. </person>
The inverse sequence has been resolved as follows: Java code
    1. person [id=,Name=huangwei, age=, Phone=phonenumber [code=222, number=15200000000]

Reproduced -xstream Parsing of XML parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.