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
- Package CN.TJPU.ZHW.XML.XSTREAM5;
- Import java.io.IOException;
- Import Java.io.ObjectOutputStream;
- Import Com.thoughtworks.xstream.XStream;
- public class Outmain {
- public static void Main (string[] args) throws IOException {
- Create a XStream Object
- XStream XStream = new XStream ();
- /*******1, serializing a single object *******/
- System.out.println ("*******1, serialization of a single object *******");
- Xstream.toxml (New person ("Zhang San"), System.out);
- System.out.println ();
- System.out.println ();
- /*******2, serializes a set of objects *******/
- System.out.println ("*******2, serializing a set of objects *******");
- Output formatted XML to System.out, root is the node
- ObjectOutputStream Oos = Xstream.createobjectoutputstream (System.out, "root");
- Oos.writeobject (New person ("Zhang San"));
- Oos.writeobject (New person ("John Doe"));
- Oos.writeobject (New Integer (1));
- Oos.writeobject (2);
- Oos.writeobject (New Double (3));
- Oos.writeobject (4d);
- Oos.writeobject (' C ');
- Oos.writeobject ("This is a bunch of strings!") ");
- Be sure to close the stream
- Oos.close ();
- }
- }
- Class Person {
- Public person (String name) {
- THIS.name = name;
- }
- private String name;
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- THIS.name = name;
- }
- Public String toString () {
- Return "name= of the Person object" +getname ();
- }
- }
Operation Result:
Java code
- 1, serializing a single object *******
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name> Zhang San </name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- 2, serializes a set of objects *******
- <root>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name> Zhang San </name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name> John Doe </name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <int>1</int>
- <int>2</int>
- <double>3.0</double>
- <double>4.0</double>
- <char>c</char>
- <string> This is a bunch of strings! </string>
- </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
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name> Zhang San </name>
- </cn.tjpu.zhw.xml.xstream5.Person>
Write the following to the D:/temp2.xml file:
XML code
- <root>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name> Zhang San </name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <cn.tjpu.zhw.xml.xstream5.Person>
- <name> John Doe </name>
- </cn.tjpu.zhw.xml.xstream5.Person>
- <int>1</int>
- <int>2</int>
- <double>3.0</double>
- <double>4.0</double>
- <char>c</char>
- <string> This is a bunch of strings! </string>
- </root>
The input stream test program is as follows:
Java code
- Package CN.TJPU.ZHW.XML.XSTREAM5;
- Import java.io.EOFException;
- Import Java.io.File;
- Import Java.io.FileReader;
- Import java.io.IOException;
- Import Java.io.ObjectInputStream;
- Import Com.thoughtworks.xstream.XStream;
- public class Inmain {
- public static void Main (string[] args) throws IOException, ClassNotFoundException {
- Create a XStream Object
- XStream XStream = new XStream ();
- /*******1, deserializes an object *******/
- FileReader reader1 = new FileReader (New File ("D:/temp1.xml"));
- Person PP = (person) xstream.fromxml (reader1);
- System.out.println ("*******1, deserialization of an object *******");
- System.out.println ("pp=" +pp);
- System.out.println ();
- System.out.println ();
- /*******1, deserializes a set of objects *******/
- FileReader reader2 = new FileReader (New File ("D:/temp2.xml"));
- ObjectInputStream ois = Xstream.createobjectinputstream (Reader2);
- person P1 = (person) ois.readobject ();
- System.out.println ("p1=" +P1);
- person P2 = (person) ois.readobject ();
- System.out.println ("p2=" +P2);
- int i1 = (Integer) ois.readobject ();
- System.out.println ("i1=" +i1);
- int i2 = (Integer) ois.readobject ();
- System.out.println ("i2=" +i2);
- Double D1 = (double) ois.readobject ();
- System.out.println ("d1=" +d1);
- Double D2 = (double) ois.readobject ();
- System.out.println ("d2=" +d2);
- char ch = (Character) ois.readobject ();
- System.out.println ("ch=" +ch);
- String str = (string) ois.readobject ();
- System.out.println ("str=" +str);
- SYSTEM.OUT.PRINTLN ("****** anomaly capture ******");
- An exception occurred
- try {
- Ois.readobject ();
- } catch (Eofexception e) {
- System.out.println ("Because there is no data, again read, will occur eofexception exception");
- }
- }
- }
Operation Result:
Java code
- 1, deserializes an object *******
- Name= Zhang San for Pp=person objects
- Name= Zhang San for P1=person objects
- Name= John Doe for P2=person objects
- I1=1
- i2=2
- d1=3.0
- d2=4.0
- Ch=c
- Str= This is a bunch of strings!
- Exception capture ******
- 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