android中Pull解析器

android中Pull解析器
pull解析器
    xml解析器有很多种,常见的有Jdom,dom4j等,pull解析xml为android系统所用到的解析器,是android自带的解析器。
preparation

    1.项目结构
    
    2.项目的代码:
Colleage.java(bean对象)












1
package com.rhg.domain;
2
3public class Colleage {
4
5private Integer id;
6private String name;
7private Integer age;
8
9public Colleage() {
10// TODO Auto-generated constructor stub
11 }

12
13public Colleage(int id,String name,int age){
14this.id = id;
15this.name = name;
16this.age = age;
17 }

18
19public Integer getId() {
20return id;
21 }

22public void setId(Integer id) {
23this.id = id;
24 }

25public String getName() {
26return name;
27 }

28public void setName(String name) {
29this.name = name;
30 }

31public Integer getAge() {
32return age;
33 }

34public void setAge(Integer age) {
35this.age = age;
36 }

37
38 @Override
39public String toString() {
40// TODO Auto-generated method stub
41return "Colleage[id="+id+",name="+name+",age="+age;
42 }

43
44}

ColleageService.java(实现解析xml和生成xml)

1package com.rhg.service;
2
3import java.io.InputStream;
4import java.io.OutputStream;
5import java.util.ArrayList;
6import java.util.List;
7
8import org.xmlpull.v1.XmlPullParser;
9import org.xmlpull.v1.XmlSerializer;
10
11import android.util.Xml;
12
13import com.rhg.domain.Colleage;
14
15public class ColleageService {
16
17public List<Colleage> read(InputStream in) throws Exception{
18 List<Colleage> colleages = null;
19 Colleage colleage = null;
20 XmlPullParser parser = Xml.newPullParser();
21 parser.setInput(in, "UTF-8");
22int event = parser.getEventType();
23while(event!=parser.END_DOCUMENT){
24switch(event){
25case XmlPullParser.START_DOCUMENT:
26 colleages = new ArrayList<Colleage>();
27break;
28case XmlPullParser.START_TAG:
29if("colleage".equals(parser.getName())){
30 Integer id = new Integer(parser.getAttributeValue(0));
31 colleage = new Colleage();
32 colleage.setId(id);
33 }

34if("name".equals(parser.getName())){
35 String name = parser.nextText();
36 colleage.setName(name);
37 }

38if("age".equals(parser.getName())){
39int age =new Integer(parser.nextText());
40 colleage.setAge(age);
41 }

42break;
43case XmlPullParser.END_TAG:
44if("colleage".equals(parser.getName())){
45 colleages.add(colleage);
46 colleage = null;
47 }

48break;
49 }

50 event = parser.next();
51 }

52return colleages;
53 }

54
55public static void save(List<Colleage> colleages,OutputStream out) throws Exception{
56 XmlSerializer xml = Xml.newSerializer();
57 xml.setOutput(out, "UTF-8");
58 xml.startDocument("UTF-8", true);
59 xml.startTag(null, "colleages");
60for(Colleage colleage:colleages){
61 xml.startTag(null, "colleage");
62 xml.attribute(null, "id", colleage.getId().toString());
63
64 xml.startTag(null, "name");
65 xml.text(colleage.getName());
66 xml.endTag(null, "name");
67
68 xml.startTag(null, "age");
69 xml.text(colleage.getAge().toString());
70 xml.endTag(null, "age");
71
72 xml.endTag(null, "colleage");
73 }

74 xml.endTag(null, "colleages");
75 xml.endDocument();
76 out.flush();
77 out.close();
78 }

79}

80


ColleageTest(用于测试生成由对象生成xml 和解析xml生成对象)

1package com.rhg.test;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.InputStream;
7import java.util.ArrayList;
8import java.util.List;
9
10import android.test.AndroidTestCase;
11import android.util.Log;
12
13import com.rhg.domain.Colleage;
14import com.rhg.service.ColleageService;
15
16public class ColleageTest extends AndroidTestCase {
17
18public void testRead(){
19 ColleageService cs = new ColleageService();
20 InputStream is = this.getContext().getClassLoader().getResourceAsStream("NewFile.xml");
21try {
22 List<Colleage> colleages = cs.read(is);
23for(Colleage colleage:colleages){
24 Log.v("colleageTest", "1-----"+colleage.toString());
25 }

26 }
catch (Exception e) {
27// TODO Auto-generated catch block
28 e.printStackTrace();
29 }

30 }

31
32public void testSave() throws Exception{
33 List<Colleage> colleages = new ArrayList<Colleage>();
34 Colleage colleage1 = new Colleage(1,"zyh1",10);
35 Colleage colleage2 = new Colleage(2,"zyh2",20);
36 Colleage colleage3 = new Colleage(3,"zyh3",30);
37 colleages.add(colleage1);
38 colleages.add(colleage2);
39 colleages.add(colleage3);
40
41 File xmlFile = new File(getContext().getFilesDir(),"zyh.xml");
42 FileOutputStream out = new FileOutputStream(xmlFile);
43 ColleageService.save(colleages, out);
44
45 }

46}

47

NewFile.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<colleages>
 3    <colleage id="1">
 4        <name>qh</name>
 5        <age>100</age>
 6    </colleage>
 7    <colleage id="2">
 8        <name>bd</name>
 9        <age>101</age>
10    </colleage>
11</colleages>

AndroidManifest.xml
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.rhg.xmldemo"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="17" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.rhg.xmldemo.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 <uses-library android:name="android.test.runner"/>
26 </application>
27
28 <instrumentation android:name="android.test.InstrumentationTestRunner"
29 android:targetPackage="com.rhg.xmldemo" android:label="Tests for My App" />
30</manifest>
31



你可能感兴趣的:(android中Pull解析器)