1 package com.gnnuit.service;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.xmlpull.v1.XmlPullParser;
10 import org.xmlpull.v1.XmlSerializer;
11
12 import com.gnnuit.domain.Person;
13
14 import android.content.Context;
15 import android.content.res.AssetManager;
16 import android.os.Environment;
17 import android.util.Xml;
18 import android.widget.Toast;
19
20 /**
21 * 使用Android的Pull解析器对XML文件进行解析
22 *
23 * @author 杨伦义
24 *
25 */
26 public class XmlService {
27 private Context context;
28
29 public XmlService(Context context) {
30 this.context = context;
31 }
32
33 /**
34 * 使用Android的Pull解析器读取XML文件
35 *
36 * @param fileName要解析的XML文件名
37 * @return JavaBean的List集合
38 */
39 public List<Person> readXml(String fileName) {
40 // 获得Asset文件
41 AssetManager manager = context.getAssets();
42 Person person = null;
43 List<Person> list = new ArrayList<Person>();
44 try {
45 InputStream is = manager.open(fileName);
46 // =====================================================================
47 // 使用Android的pull解析器解析XML文档
48 // 得到pull解析器的实例
49 XmlPullParser parser = Xml.newPullParser();
50 // 设置解析器参数
51 parser.setInput(is, "UTF-8");
52 // 得到解析器的事件类型
53 int type = parser.getEventType();
54 while (type != XmlPullParser.END_DOCUMENT) {
55 if (type == XmlPullParser.START_TAG) {
56 if ("person".equals(parser.getName())) {
57 person = new Person();
58 int id = Integer.parseInt(parser.getAttributeValue(0));
59 person.setId(id);
60 } else if ("name".equals(parser.getName())) {
61 String name = parser.nextText();
62 person.setName(name);
63 } else if ("age".equals(parser.getName())) {
64 int age = Integer.parseInt(parser.nextText());
65 person.setAge(age);
66 }
67 }
68 if (type == XmlPullParser.END_TAG) {
69 if ("person".equals(parser.getName())) {
70 list.add(person);
71 }
72 }
73 type = parser.next();
74 }
75 return list;
76 } catch (Exception e) {
77 e.printStackTrace();
78 Toast.makeText(context, "文件打开失败", Toast.LENGTH_SHORT).show();
79 }
80 return null;
81 }
82
83 /**
84 * 使用Android的Pull解析器保存XML文件
85 *
86 * @param persons要保存的List对象
87 * @return 是否保存成功
88 */
89 public boolean writeXml(List<Person> persons) {
90 try {
91 // 获得XML序列化器
92 XmlSerializer serializer = Xml.newSerializer();
93 File file = new File(Environment.getExternalStorageDirectory(),
94 "person.xml");
95 FileOutputStream fos = new FileOutputStream(file);
96 serializer.setOutput(fos, "utf-8");
97 serializer.startDocument("utf-8", true);
98 serializer.startTag(null, "persons");
99 for (Person p : persons) {
100 serializer.startTag(null, "person");
101 serializer.attribute(null, "id", p.getId() + "");
102
103 serializer.startTag(null, "name");
104 serializer.text(p.getName());
105 serializer.endTag(null, "name");
106
107 serializer.startTag(null, "age");
108 serializer.text(p.getAge() + "");
109 serializer.endTag(null, "age");
110
111 serializer.endTag(null, "person");
112 }
113 serializer.endTag(null, "persons");
114 serializer.endDocument();
115 fos.close();
116 return true;
117 } catch (Exception e) {
118 e.printStackTrace();
119 return false;
120 }
121 }
122 }