Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> pull解析器: 反序列化與序列化,pull序列化

pull解析器: 反序列化與序列化,pull序列化

編輯:關於android開發

pull解析器: 反序列化與序列化,pull序列化


 

  pull解析器:反序列化  讀取xml文件來獲取一個對象的數據

 

 1 import java.io.FileInputStream;
 2 import java.io.IOException;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.xmlpull.v1.XmlPullParser;
 7 import org.xmlpull.v1.XmlPullParserException;
 8 import org.xmlpull.v1.XmlPullParserFactory;
 9 
10 public class ReadXmlTest {
11 
12     /**
13      *   pull解析器:反序列化     讀取xml文件來獲取一個對象的數據
14      * @param args
15      * @throws XmlPullParserException 
16      * @throws IOException 
17      */
18     public static void main(String[] args) throws XmlPullParserException, IOException {
19 
20         //1.導包
21         //2.獲取解析器工廠
22         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
23         //3.根據工廠獲取解析器對象
24         XmlPullParser parser = factory.newPullParser();
25         //4.設置輸入流
26         parser.setInput(new FileInputStream("src/student.xml"),"utf-8");
27         //5.解析
28         List<Student> list = null;
29         Student stu = null;
30         //獲取對應時間的類型
31         int type = parser.getEventType(); 
32         while(type != XmlPullParser.END_DOCUMENT){
33             //獲取標簽名稱
34             String tagname = parser.getName();
35             switch(type){
36             case XmlPullParser.START_TAG:
37                 //判斷標簽名稱
38                 if("students".equals(tagname)){
39                     //創建集合
40                     list = new ArrayList<Student>();
41                 }else if("student".equals(tagname)){
42                     //創建對象
43                     stu = new Student();
44                     //獲取ID屬性值
45                     String id = parser.getAttributeValue(0);
46                     //賦值
47                     stu.setId(id);
48                 }else if("name".equals(tagname)){
49                     //獲取標簽體的文本
50                     String name = parser.nextText();
51                     //賦值
52                     stu.setName(name);
53                 }else if("age".equals(tagname)){
54                     //獲取標簽體的文本
55                     String age = parser.nextText();
56                     //賦值
57                     stu.setAge(Integer.parseInt(age));
58             }
59                 break;
60             case XmlPullParser.END_TAG:
61                 if("student".equals(tagname)){
62                     //將對象添加到集合中
63                     list.add(stu);
64                     stu = null;
65                 }
66                 break;
67             default:
68                 break;
69             }
70         //向下走一步
71         parser.next();
72         //重新賦值
73         type = parser.getEventType();
74         }
75         //6.輸出
76         System.out.println(list);
77     }
78 }

 

------------------------------------------------------------------------------------------------

 

  pull解析器:序列化    將一個對象的數據  寫入到xml文件中

 

 1 import java.io.FileOutputStream;
 2 import java.io.IOException;
 3 
 4 import org.xmlpull.v1.XmlPullParserException;
 5 import org.xmlpull.v1.XmlPullParserFactory;
 6 import org.xmlpull.v1.XmlSerializer;
 7 
 8 public class WriteXmlTest {
 9 
10     /**pull解析器:序列化   將一個對象的數據  寫入到xml文件中
11      * @param args
12      * @throws XmlPullParserException 
13      * @throws IOException 
14      */
15     public static void main(String[] args) throws XmlPullParserException, IOException {
16 
17         Student stu = new Student("s_001", "小飛飛", 23);
18         //1.導包
19         //2.獲取解析器工廠
20         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
21         //3.根據工廠獲取解析器對象
22         XmlSerializer serializer = factory.newSerializer();
23         //4.設置輸出流
24         serializer.setOutput(new FileOutputStream("src/sss.xml"),"utf-8");
25         //5.寫入
26         //5.1寫文檔聲明  參數1:encoding屬性值,參數2:standalone屬性值
27         serializer.startDocument("utf-8", true);
28         //5.2寫入開始跟標簽
29         serializer.startTag(null, "students");
30         for(int i=0; i <5; i++){
31             //5.3寫入student標簽
32             serializer.startTag(null, "student");
33             
34             serializer.attribute(null, "id", stu.getId());
35             
36             serializer.startTag(null, "name");
37             serializer.text(stu.getName());
38             serializer.endTag(null, "name");
39             
40             serializer.startTag(null, "age");
41             serializer.text(String.valueOf(stu.getAge()));
42             serializer.endTag(null, "age");
43             
44             serializer.endTag(null, "student");
45         }
46         //5.4寫入結束標簽
47         serializer.endTag(null, "students");
48         //5.5寫入結束文檔
49         serializer.endDocument();
50         //反應結果
51         System.out.println("寫入完成,請去查看!");
52     }
53 }

 

  Student類

 

 1 public class Student {
 2 
 3     private String id;
 4     private String name;
 5     private int age;
 6     
 7     public Student() {
 8         super();
 9     }
10     public Student(String id, String name, int age) {
11         super();
12         this.id = id;
13         this.name = name;
14         this.age = age;
15     }
16     /**
17      * @return the id
18      */
19     public String getId() {
20         return id;
21     }
22     /**
23      * @param id the id to set
24      */
25     public void setId(String id) {
26         this.id = id;
27     }
28     /**
29      * @return the name
30      */
31     public String getName() {
32         return name;
33     }
34     /**
35      * @param name the name to set
36      */
37     public void setName(String name) {
38         this.name = name;
39     }
40     /**
41      * @return the age
42      */
43     public int getAge() {
44         return age;
45     }
46     /**
47      * @param age the age to set
48      */
49     public void setAge(int age) {
50         this.age = age;
51     }
52     /* (non-Javadoc)
53      * @see java.lang.Object#toString()
54      */
55     @Override
56     public String toString() {
57         return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
58     }
59 
60 }

 

  student.xml

 1 <?xml version='1.0' encoding='utf-8' ?>
 2 
 3 <students>
 4     <student id='s001'>
 5         <name>zhangsan</name>
 6         <age>23</age>
 7     </student>
 8         
 9     <student id="s002">
10         <name>lisi</name>
11         <age>24</age>
12     </student>
13 
14     <student id="s003">
15         <name>王五</name>
16         <age>25</age>
17     </student>
18     
19 </students>

 

導入的包名稱(pull解析器jar包):

  kxml2-2.3.0.jar

  xmlpull_1_1_3_4c.jar

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved