Using XStream is the implementation of XML and Java Object Conversion (5)--object Stream

Source: Internet
Author: User

Eight, Object Stream

In the previous example, we are simply outputting XML as a string type or getting and parsing xml from a string, and now we are going to handle the input stream and output stream!

1, output stream (ObjectOutputStream)

The output stream test program is as follows:

Java code
  1. Package CN.TJPU.ZHW.XML.XSTREAM5;
  2. Import java.io.IOException;
  3. Import Java.io.ObjectOutputStream;
  4. Import Com.thoughtworks.xstream.XStream;
  5. public class Outmain {
  6. public static void Main (string[] args) throws IOException {
  7. Create a XStream Object
  8. XStream XStream = new XStream ();
  9. /*******1, serializing a single object *******/
  10. System.out.println ("*******1, serialization of a single object *******");
  11. Xstream.toxml (New person ("Zhang San"), System.out);
  12. System.out.println ();
  13. System.out.println ();
  14. /*******2, serializes a set of objects *******/
  15. System.out.println ("*******2, serializing a set of objects *******");
  16. Output formatted XML to System.out, root is the node
  17. ObjectOutputStream Oos = Xstream.createobjectoutputstream (System.out, "root");
  18. Oos.writeobject (New person ("Zhang San"));
  19. Oos.writeobject (New person ("John Doe"));
  20. Oos.writeobject (New Integer (1));
  21. Oos.writeobject (2);
  22. Oos.writeobject (New Double (3));
  23. Oos.writeobject (4d);
  24. Oos.writeobject (' C ');
  25. Oos.writeobject ("This is a bunch of strings!") ");
  26. Be sure to close the stream
  27. Oos.close ();
  28. }
  29. }
  30. Class Person {
  31. Public person (String name) {
  32. THIS.name = name;
  33. }
  34. private String name;
  35. Public String GetName () {
  36. return name;
  37. }
  38. public void SetName (String name) {
  39. THIS.name = name;
  40. }
  41. Public String toString () {
  42. Return "name= of the Person object" +getname ();
  43. }
  44. }

Operation Result:

Java code
  1. 1, serializing a single object *******
  2. <cn.tjpu.zhw.xml.xstream5.Person>
  3. <name> Zhang San </name>
  4. </cn.tjpu.zhw.xml.xstream5.Person>
  5. 2, serializes a set of objects *******
  6. <root>
  7. <cn.tjpu.zhw.xml.xstream5.Person>
  8. <name> Zhang San </name>
  9. </cn.tjpu.zhw.xml.xstream5.Person>
  10. <cn.tjpu.zhw.xml.xstream5.Person>
  11. <name> John Doe </name>
  12. </cn.tjpu.zhw.xml.xstream5.Person>
  13. <int>1</int>
  14. <int>2</int>
  15. <double>3.0</double>
  16. <double>4.0</double>
  17. <char>c</char>
  18. <string> This is a bunch of strings! </string>
  19. </root>

The above two examples are directly using System.out to output XML to the console. In fact, we can replace it with any output stream we like, such as FileWriter.

2, input stream (ObjectInputStream)

Write the following to the D:/temp1.xml file:

XML code
    1. <cn.tjpu.zhw.xml.xstream5.Person>
    2. <name> Zhang San </name>
    3. </cn.tjpu.zhw.xml.xstream5.Person>

Write the following to the D:/temp2.xml file:

XML code
    1. <root>
    2. <cn.tjpu.zhw.xml.xstream5.Person>
    3. <name> Zhang San </name>
    4. </cn.tjpu.zhw.xml.xstream5.Person>
    5. <cn.tjpu.zhw.xml.xstream5.Person>
    6. <name> John Doe </name>
    7. </cn.tjpu.zhw.xml.xstream5.Person>
    8. <int>1</int>
    9. <int>2</int>
    10. <double>3.0</double>
    11. <double>4.0</double>
    12. <char>c</char>
    13. <string> This is a bunch of strings! </string>
    14. </root>

The input stream test program is as follows:

Java code
  1. Package CN.TJPU.ZHW.XML.XSTREAM5;
  2. Import java.io.EOFException;
  3. Import Java.io.File;
  4. Import Java.io.FileReader;
  5. Import java.io.IOException;
  6. Import Java.io.ObjectInputStream;
  7. Import Com.thoughtworks.xstream.XStream;
  8. public class Inmain {
  9. public static void Main (string[] args) throws IOException, ClassNotFoundException {
  10. Create a XStream Object
  11. XStream XStream = new XStream ();
  12. /*******1, deserializes an object *******/
  13. FileReader reader1 = new FileReader (New File ("D:/temp1.xml"));
  14. Person PP = (person) xstream.fromxml (reader1);
  15. System.out.println ("*******1, deserialization of an object *******");
  16. System.out.println ("pp=" +pp);
  17. System.out.println ();
  18. System.out.println ();
  19. /*******1, deserializes a set of objects *******/
  20. FileReader reader2 = new FileReader (New File ("D:/temp2.xml"));
  21. ObjectInputStream ois = Xstream.createobjectinputstream (Reader2);
  22. person P1 = (person) ois.readobject ();
  23. System.out.println ("p1=" +P1);
  24. person P2 = (person) ois.readobject ();
  25. System.out.println ("p2=" +P2);
  26. int i1 = (Integer) ois.readobject ();
  27. System.out.println ("i1=" +i1);
  28. int i2 = (Integer) ois.readobject ();
  29. System.out.println ("i2=" +i2);
  30. Double D1 = (double) ois.readobject ();
  31. System.out.println ("d1=" +d1);
  32. Double D2 = (double) ois.readobject ();
  33. System.out.println ("d2=" +d2);
  34. char ch = (Character) ois.readobject ();
  35. System.out.println ("ch=" +ch);
  36. String str = (string) ois.readobject ();
  37. System.out.println ("str=" +str);
  38. SYSTEM.OUT.PRINTLN ("****** anomaly capture ******");
  39. An exception occurred
  40. try {
  41. Ois.readobject ();
  42. } catch (Eofexception e) {
  43. System.out.println ("Because there is no data, again read, will occur eofexception exception");
  44. }
  45. }
  46. }

Operation Result:

Java code
    1. 1, deserializes an object *******
    2. Name= Zhang San for Pp=person objects
    3. Name= Zhang San for P1=person objects
    4. Name= John Doe for P2=person objects
    5. I1=1
    6. i2=2
    7. d1=3.0
    8. d2=4.0
    9. Ch=c
    10. Str= This is a bunch of strings!
    11. Exception capture ******
    12. Because there is no data, the eofexception exception occurs when you read it again.

Using XStream is the implementation of XML and Java Object Conversion (5)--object Stream

Related Article

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.