除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android内置的Pull解析器解析XML文件。 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。
Pull解析器的源码及文档下载网址:http://www.xmlpull.org/
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中;或者使用DOM API生成XML文件,或者也可以使用pull解析器生成XML文件,这里推荐大家使用Pull解析器。
我们在android的工程下写代码测试,使用android的单元测试技术进行测试,当然测试类还要继承AndroidTestCase
在layout/res/AndroidManifest.xml文件下添加单元测试所需要的代码:
在AndroidManifest.xml文件的application节点下添加以下代码:
<uses-library android:name="android.test.runner" />
在AndroidManifest.xml文件的manifest节点下添加以下代码:
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="你的测试文件所在的包名" android:label="Tests for My App" />
同样我们使用一个bean类来作为数据的接收,
Person.java
1
package
com.gaolei.domain;
2
3 public class Person {
4
5 private Integer id;
6 private String name;
7 private Integer age;
8
9 public Integer getId() {
10 return id;
11 }
12
13 public void setId(Integer id) {
14 this .id = id;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this .name = name;
23 }
24
25 public Integer getAge() {
26 return age;
27 }
28
29 public void setAge(Integer age) {
30 this .age = age;
31 }
32
33 @Override
34 public String toString() {
35 return " Person [id= " + id + " , name= " + name + " , age= " + age + " ] " ;
36 }
37
38 public Person(Integer id, String name, Integer age) {
39 this .id = id;
40 this .name = name;
41 this .age = age;
42 }
43
44 public Person() {
45 }
46
47 }
48
2
3 public class Person {
4
5 private Integer id;
6 private String name;
7 private Integer age;
8
9 public Integer getId() {
10 return id;
11 }
12
13 public void setId(Integer id) {
14 this .id = id;
15 }
16
17 public String getName() {
18 return name;
19 }
20
21 public void setName(String name) {
22 this .name = name;
23 }
24
25 public Integer getAge() {
26 return age;
27 }
28
29 public void setAge(Integer age) {
30 this .age = age;
31 }
32
33 @Override
34 public String toString() {
35 return " Person [id= " + id + " , name= " + name + " , age= " + age + " ] " ;
36 }
37
38 public Person(Integer id, String name, Integer age) {
39 this .id = id;
40 this .name = name;
41 this .age = age;
42 }
43
44 public Person() {
45 }
46
47 }
48
person.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 < persons >
3 < person id ="23" >
4 < name > gaolei </ name >
5 < age > 30 </ age >
6 </ person >
7 < person id ="20" >
8 < name > xiaoqiang </ name >
9 < age > 25 </ age >
10 </ person >
11 </ persons >
12
2 < persons >
3 < person id ="23" >
4 < name > gaolei </ name >
5 < age > 30 </ age >
6 </ person >
7 < person id ="20" >
8 < name > xiaoqiang </ name >
9 < age > 25 </ age >
10 </ person >
11 </ persons >
12
PullPersonService pull解析器操作类
1
package
com.gaolei.service;
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.xmlpull.v1.XmlPullParser;
9 import org.xmlpull.v1.XmlSerializer;
10
11 import android.util.Xml;
12
13 import com.gaolei.domain.Person;
14
15 public class PersonService {
16
17 /**
18 * 获取数据
19 *
20 * @param xml
21 * @return
22 * @throws Exception
23 */
24 public static List < Person > getPersons(InputStream xml) throws Exception {
25 List < Person > persons = null ;
26 Person person = null ;
27 XmlPullParser pullParser = Xml.newPullParser();
28 pullParser.setInput(xml, " UTF-8 " ); // 为Pull解析器设置要解析的XML数据
29 int event = pullParser.getEventType();
30 while (event != XmlPullParser.END_DOCUMENT) {
31 switch (event) {
32 case XmlPullParser.START_DOCUMENT: // 文档开始事件,可以进行数据初始化处理
33 persons = new ArrayList < Person > ();
34 break ;
35 case XmlPullParser.START_TAG:
36 if ( " person " .equals(pullParser.getName())) {
37 int id = Integer.parseInt(pullParser.getAttributeValue( 0 ));
38 person = new Person();
39 person.setId(id);
40 }
41 if ( " name " .equals(pullParser.getName())) {
42 String name = pullParser.nextText(); // 如果后面是Text元素,即返回它的值
43 person.setName(name);
44 }
45 if ( " age " .equals(pullParser.getName())) {
46 int age = new Integer(pullParser.nextText());
47 person.setAge(age);
48 }
49 break ;
50 case XmlPullParser.END_TAG: // 结束元素事件
51 if ( " person " .equals(pullParser.getName())) {
52 persons.add(person);
53 person = null ;
54 }
55 }
56 event = pullParser.next();
57 }
58 return persons;
59 }
60
61 /**
62 * 保存数据
63 *
64 * @param persons
65 * 数据
66 * @param os
67 * 方向
68 * @throws Exception
69 */
70 public static void save(List < Person > persons, OutputStream os)
71 throws Exception {
72 XmlSerializer serializer = Xml.newSerializer();
73 serializer.setOutput(os, " UTF-8 " );
74 serializer.startDocument( " UTF-8 " , true );
75 // 第一个参数为命名空间,如果不使用命名空间,可以设置为null
76 serializer.startTag( null , " persons " );
77 for (Person person : persons) {
78 serializer.startTag( null , " person " );
79 serializer.attribute( null , " id " , person.getId().toString());
80
81 serializer.startTag( null , " name " );
82 serializer.text(person.getName());
83 serializer.endTag( null , " name " );
84
85 serializer.startTag( null , " age " );
86 serializer.text(person.getAge().toString());
87 serializer.endTag( null , " age " );
88
89 serializer.endTag( null , " person " );
90 }
91 serializer.endTag( null , " persons " );
92 serializer.endDocument();
93 os.flush();
94 os.close();
95
96 }
97 }
98
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.xmlpull.v1.XmlPullParser;
9 import org.xmlpull.v1.XmlSerializer;
10
11 import android.util.Xml;
12
13 import com.gaolei.domain.Person;
14
15 public class PersonService {
16
17 /**
18 * 获取数据
19 *
20 * @param xml
21 * @return
22 * @throws Exception
23 */
24 public static List < Person > getPersons(InputStream xml) throws Exception {
25 List < Person > persons = null ;
26 Person person = null ;
27 XmlPullParser pullParser = Xml.newPullParser();
28 pullParser.setInput(xml, " UTF-8 " ); // 为Pull解析器设置要解析的XML数据
29 int event = pullParser.getEventType();
30 while (event != XmlPullParser.END_DOCUMENT) {
31 switch (event) {
32 case XmlPullParser.START_DOCUMENT: // 文档开始事件,可以进行数据初始化处理
33 persons = new ArrayList < Person > ();
34 break ;
35 case XmlPullParser.START_TAG:
36 if ( " person " .equals(pullParser.getName())) {
37 int id = Integer.parseInt(pullParser.getAttributeValue( 0 ));
38 person = new Person();
39 person.setId(id);
40 }
41 if ( " name " .equals(pullParser.getName())) {
42 String name = pullParser.nextText(); // 如果后面是Text元素,即返回它的值
43 person.setName(name);
44 }
45 if ( " age " .equals(pullParser.getName())) {
46 int age = new Integer(pullParser.nextText());
47 person.setAge(age);
48 }
49 break ;
50 case XmlPullParser.END_TAG: // 结束元素事件
51 if ( " person " .equals(pullParser.getName())) {
52 persons.add(person);
53 person = null ;
54 }
55 }
56 event = pullParser.next();
57 }
58 return persons;
59 }
60
61 /**
62 * 保存数据
63 *
64 * @param persons
65 * 数据
66 * @param os
67 * 方向
68 * @throws Exception
69 */
70 public static void save(List < Person > persons, OutputStream os)
71 throws Exception {
72 XmlSerializer serializer = Xml.newSerializer();
73 serializer.setOutput(os, " UTF-8 " );
74 serializer.startDocument( " UTF-8 " , true );
75 // 第一个参数为命名空间,如果不使用命名空间,可以设置为null
76 serializer.startTag( null , " persons " );
77 for (Person person : persons) {
78 serializer.startTag( null , " person " );
79 serializer.attribute( null , " id " , person.getId().toString());
80
81 serializer.startTag( null , " name " );
82 serializer.text(person.getName());
83 serializer.endTag( null , " name " );
84
85 serializer.startTag( null , " age " );
86 serializer.text(person.getAge().toString());
87 serializer.endTag( null , " age " );
88
89 serializer.endTag( null , " person " );
90 }
91 serializer.endTag( null , " persons " );
92 serializer.endDocument();
93 os.flush();
94 os.close();
95
96 }
97 }
98
PullPersonServiceTest pull解析器测试类
1
package
com.gaolei.test;
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 android.test.AndroidTestCase;
10 import android.util.Log;
11
12 import com.gaolei.domain.Person;
13 import com.gaolei.service.PersonService;
14
15 public class PersonServicTest extends AndroidTestCase {
16 public static final String TAG = " PersonServicTest " ;
17
18 /**
19 * 使用pull解析器解析xml文件
20 * @throws Exception
21 */
22 public void testCase() throws Exception {
23
24 InputStream xml = this .getClass().getClassLoader()
25 .getResourceAsStream( " person.xml " );
26
27 List < Person > persons = PersonService.getPersons(xml);
28 for (Person person : persons) {
29 Log.i(TAG, person.toString());
30 }
31 }
32
33 /**
34 * 使用pull解析器生成xml文件
35 * @throws Exception
36 */
37 public void testSaveCase() throws Exception {
38
39 List < Person > persons = new ArrayList < Person > ();
40 persons.add( new Person( 1 , " zhangsan " , 21 ));
41 persons.add( new Person( 2 , " lisi " , 22 ));
42 persons.add( new Person( 3 , " wangwu " , 23 ));
43
44 // <包>/files
45 File xmlFile = new File(getContext().getFilesDir(), " gaolei.xml " );
46 FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);
47 PersonService.save(persons, fileOutputStream);
48 }
49 }
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 android.test.AndroidTestCase;
10 import android.util.Log;
11
12 import com.gaolei.domain.Person;
13 import com.gaolei.service.PersonService;
14
15 public class PersonServicTest extends AndroidTestCase {
16 public static final String TAG = " PersonServicTest " ;
17
18 /**
19 * 使用pull解析器解析xml文件
20 * @throws Exception
21 */
22 public void testCase() throws Exception {
23
24 InputStream xml = this .getClass().getClassLoader()
25 .getResourceAsStream( " person.xml " );
26
27 List < Person > persons = PersonService.getPersons(xml);
28 for (Person person : persons) {
29 Log.i(TAG, person.toString());
30 }
31 }
32
33 /**
34 * 使用pull解析器生成xml文件
35 * @throws Exception
36 */
37 public void testSaveCase() throws Exception {
38
39 List < Person > persons = new ArrayList < Person > ();
40 persons.add( new Person( 1 , " zhangsan " , 21 ));
41 persons.add( new Person( 2 , " lisi " , 22 ));
42 persons.add( new Person( 3 , " wangwu " , 23 ));
43
44 // <包>/files
45 File xmlFile = new File(getContext().getFilesDir(), " gaolei.xml " );
46 FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);
47 PersonService.save(persons, fileOutputStream);
48 }
49 }