使用XStream是實現XML與Java對象的轉換(5)--Object Stream

來源:互聯網
上載者:User

標籤:getname   直接   實現   character   使用   ade   private   temp   -o   

八,Object Stream

 之前的例子我們都是直接輸出Xml成為String類型或者從String中獲得並解析Xml,現在我們要處理輸入資料流和輸出資料流!

1,輸出資料流(ObjectOutputStream)

輸出資料流測試程式如下:

Java代碼  
  1. package cn.tjpu.zhw.xml.xstream5;  
  2.    
  3. import java.io.IOException;  
  4. import java.io.ObjectOutputStream;  
  5.    
  6. import com.thoughtworks.xstream.XStream;  
  7.    
  8. public class OutMain {  
  9.    
  10.    public static void main(String[] args) throws IOException {  
  11.       //建立XStream對象  
  12.       XStream xstream = new XStream();  
  13.        
  14.       /*******1,序列化單個對象*******/  
  15.       System.out.println("*******1,序列化單個對象*******");  
  16.       xstream.toXML(new Person("張三"), System.out);  
  17.        
  18.       System.out.println();  
  19.       System.out.println();  
  20.        
  21.       /*******2,序列化一組對象*******/  
  22.       System.out.println("*******2,序列化一組對象*******");  
  23.       //將格式化後的xml輸出到System.out中,root是根節點  
  24.       ObjectOutputStream oos = xstream.createObjectOutputStream(System.out,"root");  
  25.       oos.writeObject(new Person("張三"));  
  26.       oos.writeObject(new Person("李四"));  
  27.       oos.writeObject(new Integer(1));  
  28.       oos.writeObject(2);  
  29.       oos.writeObject(new Double(3));  
  30.       oos.writeObject(4d);  
  31.       oos.writeObject(‘c‘);  
  32.       oos.writeObject("這是一堆字串!");  
  33.       //一定要關閉流  
  34.       oos.close();  
  35.    }  
  36. }  
  37.    
  38. class Person {  
  39.    public Person(String name){  
  40.       this.name = name;  
  41.    }  
  42.    private String name;  
  43.    
  44.    public String getName() {  
  45.       return name;  
  46.    }  
  47.    
  48.    public void setName(String name) {  
  49.       this.name = name;  
  50.    }  
  51.    
  52.    public String toString() {  
  53.       return "Person對象的name="+getName();  
  54.    }  
  55. }  

 

 

運行結果:

Java代碼  
  1. *******1,序列化單個對象*******  
  2. <cn.tjpu.zhw.xml.xstream5.Person>  
  3.   <name>張三</name>  
  4. </cn.tjpu.zhw.xml.xstream5.Person>  
  5.    
  6. *******2,序列化一組對象*******  
  7. <root>  
  8.   <cn.tjpu.zhw.xml.xstream5.Person>  
  9.     <name>張三</name>  
  10.   </cn.tjpu.zhw.xml.xstream5.Person>  
  11.   <cn.tjpu.zhw.xml.xstream5.Person>  
  12.     <name>李四</name>  
  13.   </cn.tjpu.zhw.xml.xstream5.Person>  
  14.   <int>1</int>  
  15.   <int>2</int>  
  16.   <double>3.0</double>  
  17.   <double>4.0</double>  
  18.   <char>c</char>  
  19.   <string>這是一堆字串!</string>  
  20. </root>  

 

 上面兩個例子都是直接使用System.out,將XML輸出到控制台。其實我們可以將其替換成我們喜歡的任何輸出資料流,比如FileWriter等。

2,輸入資料流(ObjectInputStream)

將如下內容寫入D:/temp1.xml檔案:

Xml代碼  
  1. <cn.tjpu.zhw.xml.xstream5.Person>  
  2.   <name>張三</name>  
  3. </cn.tjpu.zhw.xml.xstream5.Person>  

 

 

將如下內容寫入D:/temp2.xml檔案:

Xml代碼  
  1. <root>  
  2.   <cn.tjpu.zhw.xml.xstream5.Person>  
  3.     <name>張三</name>  
  4.   </cn.tjpu.zhw.xml.xstream5.Person>  
  5.   <cn.tjpu.zhw.xml.xstream5.Person>  
  6.     <name>李四</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>這是一堆字串!</string>  
  14. </root>  

 

 

輸入資料流測試程式如下:

Java代碼  
  1. package cn.tjpu.zhw.xml.xstream5;  
  2.    
  3. import java.io.EOFException;  
  4. import java.io.File;  
  5. import java.io.FileReader;  
  6. import java.io.IOException;  
  7. import java.io.ObjectInputStream;  
  8.    
  9. import com.thoughtworks.xstream.XStream;  
  10.    
  11. public class InMain {  
  12.    
  13.    public static void main(String[] args) throws IOException, ClassNotFoundException {  
  14.        
  15.       //建立XStream對象  
  16.       XStream xstream = new XStream();  
  17.        
  18.       /*******1,還原序列化一個對象*******/  
  19.       FileReader reader1 = new FileReader(new File("D:/temp1.xml"));  
  20.       Person pp = (Person)xstream.fromXML(reader1);  
  21.       System.out.println("*******1,還原序列化一個對象*******");  
  22.       System.out.println("pp="+pp);  
  23.        
  24.        
  25.       System.out.println();  
  26.       System.out.println();  
  27.        
  28.       /*******1,還原序列化一組對象*******/  
  29.       FileReader reader2 = new FileReader(new File("D:/temp2.xml"));  
  30.       ObjectInputStream ois = xstream.createObjectInputStream(reader2);  
  31.        
  32.       Person p1 = (Person)ois.readObject();  
  33.       System.out.println("p1="+p1);  
  34.        
  35.       Person p2 = (Person)ois.readObject();  
  36.       System.out.println("p2="+p2);  
  37.        
  38.       int i1 = (Integer)ois.readObject();  
  39.       System.out.println("i1="+i1);  
  40.        
  41.       int i2 = (Integer)ois.readObject();  
  42.       System.out.println("i2="+i2);  
  43.        
  44.       double d1 = (Double)ois.readObject();  
  45.       System.out.println("d1="+d1);  
  46.        
  47.       double d2 = (Double)ois.readObject();  
  48.       System.out.println("d2="+d2);  
  49.        
  50.       char ch = (Character)ois.readObject();  
  51.       System.out.println("ch="+ch);  
  52.        
  53.       String str = (String)ois.readObject();  
  54.       System.out.println("str="+str);  
  55.        
  56.       System.out.println("******異常捕獲******");  
  57.       //發生異常  
  58.       try {  
  59.         ois.readObject();  
  60.       } catch (EOFException e) {  
  61.         System.out.println("因為已經沒有資料了,再次讀取時,就會發生EOFException異常");  
  62.       }  
  63.    }  
  64.    
  65. }  

 

運行結果:

 

Java代碼  
  1. *******1,還原序列化一個對象*******  
  2. pp=Person對象的name=張三  
  3.    
  4.    
  5. p1=Person對象的name=張三  
  6. p2=Person對象的name=李四  
  7. i1=1  
  8. i2=2  
  9. d1=3.0  
  10. d2=4.0  
  11. ch=c  
  12. str=這是一堆字串!  
  13. ******異常捕獲******  
  14. 因為已經沒有資料了,再次讀取時,就會發生EOFException異常 

使用XStream是實現XML與Java對象的轉換(5)--Object Stream

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.