首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

使用XStream是实现XML与Java对象的变换(5)-Object Stream

2013-12-28 
使用XStream是实现XML与Java对象的转换(5)--Object Stream八,Object Streampackage cn.tjpu.zhw.xml.xstre

使用XStream是实现XML与Java对象的转换(5)--Object Stream
八,Object Stream

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 { //创建XStream对象 XStream xstream = new XStream(); /*******1,序列化单个对象*******/ System.out.println("*******1,序列化单个对象*******"); xstream.toXML(new Person("张三"), System.out); System.out.println(); System.out.println(); /*******2,序列化一组对象*******/ System.out.println("*******2,序列化一组对象*******"); //将格式化后的xml输出到System.out中,root是根节点 ObjectOutputStream oos = xstream.createObjectOutputStream(System.out,"root"); oos.writeObject(new Person("张三")); oos.writeObject(new Person("李四")); oos.writeObject(new Integer(1)); oos.writeObject(2); oos.writeObject(new Double(3)); oos.writeObject(4d); oos.writeObject('c'); oos.writeObject("这是一堆字符串!"); //一定要关闭流 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 "Person对象的name="+getName(); }}

*******1,序列化单个对象*******<cn.tjpu.zhw.xml.xstream5.Person> <name>张三</name></cn.tjpu.zhw.xml.xstream5.Person> *******2,序列化一组对象*******<root> <cn.tjpu.zhw.xml.xstream5.Person> <name>张三</name> </cn.tjpu.zhw.xml.xstream5.Person> <cn.tjpu.zhw.xml.xstream5.Person> <name>李四</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>这是一堆字符串!</string></root>

<cn.tjpu.zhw.xml.xstream5.Person> <name>张三</name></cn.tjpu.zhw.xml.xstream5.Person>

<root> <cn.tjpu.zhw.xml.xstream5.Person> <name>张三</name> </cn.tjpu.zhw.xml.xstream5.Person> <cn.tjpu.zhw.xml.xstream5.Person> <name>李四</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>这是一堆字符串!</string></root>

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 { //创建XStream对象 XStream xstream = new XStream(); /*******1,反序列化一个对象*******/ FileReader reader1 = new FileReader(new File("D:/temp1.xml")); Person pp = (Person)xstream.fromXML(reader1); System.out.println("*******1,反序列化一个对象*******"); System.out.println("pp="+pp); System.out.println(); System.out.println(); /*******1,反序列化一组对象*******/ 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("******异常捕获******"); //发生异常 try { ois.readObject(); } catch (EOFException e) { System.out.println("因为已经没有数据了,再次读取时,就会发生EOFException异常"); } } }

*******1,反序列化一个对象*******pp=Person对象的name=张三 p1=Person对象的name=张三p2=Person对象的name=李四i1=1i2=2d1=3.0d2=4.0ch=cstr=这是一堆字符串!******异常捕获******因为已经没有数据了,再次读取时,就会发生EOFException异常

?

热点排行