android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化

Android内置了PULL解析器的XPP3实现,以及SAX解析器等,可以直接使用PULL或SAX解析XML,直接把JAVA中进行PULL或SAX解析的代码直接拿来用,遗忘的话,可以参考java拾遗1,2,3关于XML的解析:

如有如下XmlUtils类实现PULL方式解析XML到List和序列化List到XML:

Student实体类代码:

 1 package cn.csc.bean;
 2 
 3 public class Student {
 4 
 5       private String id;
 6 
 7       private String name;
 8 
 9       private String gender;
10 
11       private int age;
12 
13       public String getId() {
14 
15            return id;
16 
17       }
18 
19       public void setId(String id) {
20 
21            this.id = id;
22 
23       }
24 
25       public String getName() {
26 
27            return name;
28 
29       }
30 
31       public void setName(String name) {
32 
33            this.name = name;
34 
35       }
36 
37       public String getGender() {
38 
39            return gender;
40 
41       }
42 
43       public void setGender(String gender) {
44 
45            this.gender = gender;
46 
47       }
48 
49       public int getAge() {
50 
51            return age;
52 
53       }
54 
55       public void setAge(int age) {
56 
57            this.age = age;
58 
59       }
60 
61      
62 
63       public Student() {
64 
65            super();
66 
67       }
68 
69       public Student(String id, String name, String gender, int age) {
70 
71            super();
72 
73            this.id = id;
74 
75            this.name = name;
76 
77            this.gender = gender;
78 
79            this.age = age;
80 
81       }
82 
83       @Override
84 
85       public String toString() {
86 
87            return "["+id+","+name+","+gender+","+age+"]";
88 
89       }
90 
91 }

XmlUtils类的代码:

  1 public class XmlUtils {
  2 
  3       public static void serialize(Context context, String filename, List<Student> list){
  4 
  5            try {
  6 
  7                  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  8 
  9                  XmlSerializer serializer = factory.newSerializer();
 10 
 11                  serializer.setOutput(context.openFileOutput(filename, Context.MODE_PRIVATE), "utf-8");
 12 
 13                  serializer.startDocument("utf-8", true);
 14 
 15                  serializer.startTag(null, "students");
 16 
 17                  for(Student s:list){
 18 
 19                       serializer.startTag(null, "student");
 20 
 21                       serializer.attribute(null, "id", s.getId());
 22 
 23                       serializer.startTag(null, "name");
 24 
 25                       serializer.text(s.getName());
 26 
 27                       serializer.endTag(null, "name");
 28 
 29                       serializer.startTag(null, "gender");
 30 
 31                       serializer.text(s.getGender());
 32 
 33                       serializer.endTag(null, "gender");
 34 
 35                       serializer.startTag(null, "age");
 36 
 37                       serializer.text(s.getAge()+"");
 38 
 39                       serializer.endTag(null, "age");
 40 
 41                       serializer.endTag(null, "student");
 42 
 43                  }
 44 
 45                  serializer.endTag(null, "students");
 46 
 47                  serializer.endDocument();
 48 
 49            } catch (Exception e) {
 50 
 51                  // TODO Auto-generated catch block
 52 
 53                  e.printStackTrace();
 54 
 55            }
 56 
 57       }
 58 
 59       public static List<Student> parse(Context context, String filename){
 60 
 61            List<Student> list = new ArrayList<Student>();
 62 
 63            try {
 64 
 65                  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
 66 
 67                  XmlPullParser parser = factory.newPullParser();
 68 
 69                  parser.setInput(context.openFileInput(filename), "utf-8");
 70 
 71                  Student s = null;
 72 
 73                  int eventType = parser.getEventType();
 74 
 75                  while(eventType != XmlPullParser.END_DOCUMENT){
 76 
 77                       if(eventType == XmlPullParser.START_TAG){
 78 
 79                             String tag = parser.getName();
 80 
 81                             if(tag.equals("student")){
 82 
 83                                   s = new Student();
 84 
 85                                   s.setId(parser.getAttributeValue(0));
 86 
 87                             }else if(tag.equals("name")){
 88 
 89                                   s.setName(parser.nextText());
 90 
 91                             }else if(tag.equals("gender")){
 92 
 93                                   s.setGender(parser.nextText());
 94 
 95                             }else if(tag.equals("age")){
 96 
 97                                   s.setAge(Integer.parseInt(parser.nextText()));
 98 
 99                             }
100 
101                       }else if(eventType == XmlPullParser.END_TAG && parser.getName().equals("student")){
102 
103                             list.add(s);
104 
105                       }
106 
107                       eventType = parser.next();
108 
109                  }
110 
111                  for(Student stu : list){
112 
113                       Log.i("XML_PARSER",stu.toString());
114 
115                  }
116 
117            } catch (Exception e) {
118 
119                  // TODO Auto-generated catch block
120 
121                  e.printStackTrace();
122 
123                  return null;
124 
125            }
126 
127            return list;
128 
129       }
130 
131 }

使用Android Juit分别测试这两个方法:

 1 public class XmlTest extends AndroidTestCase {
 2 
 3       public void testSerializer(){
 4 
 5            List<Student> list = new ArrayList<Student>();
 6 
 7            for(int i=0; i<20; i++){
 8 
 9                  list.add(new Student(i+"","dqrcsc","male",25));
10 
11            }
12 
13            XmlUtils.serialize(getContext(), "students.xml", list);
14 
15       }
16 
17       public void testParser(){
18 
19            XmlUtils.parse(getContext(), "students.xml");
20 
21       }
22 
23 }

运行结果:

 

以上代码,除了获取Context上下文对象外,与Java项目中对XML的PULL解析是完全一样的。

实际上,Andoid在android.util包中还提供了一个Xml类,实现了对XML解析、序列化操作的封装,在使用时,可以相对简单一点:

 

这五个静态方法分别简化了PULL解析器、PULL序列化器、SAX解析的操作:

之前通过工厂类实例获取PULL序列化器的操作,可以从:

1 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
2 
3 XmlSerializer serializer = factory.newSerializer();

简化为:

XmlSerializer serializer = Xml.newSerializer();

 

通过工厂类实例获取PULL解析器的操作,也可以从:

1 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
2 
3 XmlPullParser parser = factory.newPullParser();

简化为:

XmlPullParser parser = Xml.newPullParser();

 

同样的,SAX解析也简化为直接调用Xml的parse()静态方法,这里就不说明了。

 

posted on 2015-07-10 09:46  某人的喵星人  阅读(678)  评论(0编辑  收藏  举报