005_01XML_Serilizer

将javabean的成员变量序列化保存到xml文件中,用于存储数据到本地。
Book.java
 1 package com.example.bean;
 2 
 3 public class Book {
 4 
 5      private String bookname;
 6      private String author;
 7      private String priceString;
 8      
 9      
10     @Override
11     public String toString() {
12         return "Book [bookname=" + bookname + ", author=" + author
13                 + ", priceString=" + priceString + "]";
14     }
15     
16     public String getBookname() {
17         return bookname;
18     }
19     public void setBookname(String bookname) {
20         this.bookname = bookname;
21     }
22     public String getAuthor() {
23         return author;
24     }
25     public void setAuthor(String author) {
26         this.author = author;
27     }
28     public String getPriceString() {
29         return priceString;
30     }
31     public void setPriceString(String priceString) {
32         this.priceString = priceString;
33     }
34 
35     public Book(String bookname, String author, String priceString) {
36         super();
37         this.bookname = bookname;
38         this.author = author;
39         this.priceString = priceString;
40     }
41      
42      
43 }
MainActivity.java
 1 package com.example.day_05xmlserilizer;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 import org.xmlpull.v1.XmlSerializer;
10 
11 import com.example.bean.Book;
12 
13 import android.app.Activity;
14 import android.os.Bundle;
15 import android.util.Xml;
16 import android.view.Menu;
17 import android.view.MenuItem;
18 
19 public class MainActivity extends Activity {
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         
26         savebeantoxml();
27     }
28 
29     private void savebeantoxml() {
30         Book book1 = new Book("浪潮之巅", "吴军", "30");
31         Book book2 = new Book("数学之美", "吴军", "20");
32         
33         List<Book> bookList = new ArrayList<Book>();
34         bookList.add(book1);
35         bookList.add(book2);
36         
37         XmlSerializer xs = Xml.newSerializer();
38         FileOutputStream fos = null;
39         
40         File file = new File(getFilesDir(), "books.xml");
41         
42         
43         try {
44             fos = new FileOutputStream(file);
45             xs.setOutput(fos, "utf-8");
46             xs.startDocument("utf-8", true);
47             xs.startTag(null, "books");
48             for(Book book:bookList){
49                 xs.startTag(null, "book");
50                     //bookname节点
51                     xs.startTag(null, "bookname");
52                     xs.attribute(null, "id", "01");
53                     xs.text(book.getBookname());
54                     xs.endTag(null, "bookname");
55                     //author节点
56                     xs.startTag(null, "author");
57                     xs.text(book.getAuthor());
58                     xs.endTag(null, "author");
59                     //price节点
60                     xs.startTag(null, "price");
61                     xs.text(book.getPriceString());
62                     xs.endTag(null, "price");
63                 xs.endTag(null, "book");        
64             }
65             xs.endTag(null, "books");
66             xs.endDocument();
67         } catch (IllegalArgumentException e) {
68             e.printStackTrace();
69         } catch (IllegalStateException e) {
70             e.printStackTrace();
71         } catch (IOException e) {        
72             e.printStackTrace();
73         }
74     }
75 }
物随心转,境由心造,一切烦恼皆由心生。
原文地址:https://www.cnblogs.com/woodrow2015/p/4515771.html