Android笔记(四十六) Android中的数据存储——XML(二)PULL解析

PULL 的工作原理:

XML pull提供了开始元素和结束元素。当某个元素开始时,可以调用parser.nextText()从XML文档中提取所有字符数据。当解析到一个文档结束时,自动生成EndDocument事件。

类和接口  功能
XmlPullParser 该解析器是一个在org.xmlpull.v1中定义的解析功能的接口。
XmlSerializer 它是一个接口,定义了XML信息集的序列。
XmlPullParserFactory 这个类用于在XMPULL V1 API中创建XML Pull解析器。
XmlPullParserException 抛出单一的XML pull解析器相关的错误。

它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。

读取到xml的声明返回 START_DOCUMENT

结束返回 END_DOCUMENT

开始标签返回 START_TAG

结束标签返回 END_TAG

示例:

MainActivity.java

package cn.lixyz.xmltest.activity;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import cn.lixyz.xmltest.R;
import cn.lixyz.xmltest.model.Student;
import cn.lixyz.xmltest.parser.DOMForXML;
import cn.lixyz.xmltest.parser.PULLForXML;
import cn.lixyz.xmltest.parser.SAXForXML; public class MainActivity extends Activity { private Button bt_readxml_dom, bt_writexml_dom, bt_writexml_sax, bt_readxml_sax, bt_readxml_pull, bt_writexml_pull; private List<Student> list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findView();
} private void findView() {
bt_readxml_dom = (Button) findViewById(R.id.bt_readxml_dom);
bt_writexml_dom = (Button) findViewById(R.id.bt_writexml_dom);
bt_writexml_sax = (Button) findViewById(R.id.bt_writexml_sax);
bt_readxml_sax = (Button) findViewById(R.id.bt_readxml_sax);
bt_readxml_pull = (Button) findViewById(R.id.bt_readxml_pull);
bt_writexml_pull = (Button) findViewById(R.id.bt_writexml_pull);
} public void clickButton(View view) {
DOMForXML domForXML;
SAXForXML saxForXML;
PULLForXML pullForXML;
switch (view.getId()) {
case R.id.bt_readxml_dom:
domForXML = new DOMForXML();
domForXML.domReadXML();
break; case R.id.bt_writexml_dom:
domForXML = new DOMForXML();
domForXML.domWriterXML();
break;
case R.id.bt_readxml_sax:
saxForXML = new SAXForXML();
saxForXML.saxReaderXML();
break;
case R.id.bt_writexml_sax:
saxForXML = new SAXForXML();
saxForXML.saxWriterXML();
break;
case R.id.bt_writexml_pull:
pullForXML = new PULLForXML();
pullForXML.pullWriterXML();
break;
case R.id.bt_readxml_pull:
pullForXML = new PULLForXML();
pullForXML.pullReaderXML();
break;
}
} }

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.lixyz.xmltest.activity.MainActivity" > <Button
android:id="@+id/bt_writexml_dom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="DOM方式生成XML" /> <Button
android:id="@+id/bt_readxml_dom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="DOM方式读取XML" /> <Button
android:id="@+id/bt_writexml_sax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="SAX方式生成XML" /> <Button
android:id="@+id/bt_readxml_sax"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="SAX方式读取XML" /> <Button
android:id="@+id/bt_writexml_pull"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="PULL生成XML" /> <Button
android:id="@+id/bt_readxml_pull"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clickButton"
android:text="PULL解析XML" />
</LinearLayout> </ScrollView>

PULLForXML.java

package cn.lixyz.xmltest.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer; import android.util.Log;
import android.util.Xml;
import cn.lixyz.xmltest.model.Student; public class PULLForXML { // pull生成xml文件
public void pullWriterXML() { //创建数据源
List<Student> listData = createStudents();
File fileFolder = new File("data/data/cn.lixyz.xmltest/files");
if (!fileFolder.exists()) {
fileFolder.mkdir();
Log.d("TTTT", "文件夹不存在,已经创建");
} FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream("data/data/cn.lixyz.xmltest/files/studentForPull.xml"); XmlSerializer xmlSerializer = Xml.newSerializer();
xmlSerializer.setOutput(fileOutputStream, "UTF-8"); xmlSerializer.startTag(null, "students"); for (Student student : listData) {
xmlSerializer.startTag(null, "student");
xmlSerializer.attribute("", "id", student.getId() + ""); xmlSerializer.startTag(null, "name");
xmlSerializer.text(student.getName());
xmlSerializer.endTag(null, "name"); xmlSerializer.startTag(null, "age");
xmlSerializer.text(student.getAge() + "");
xmlSerializer.endTag(null, "age");
xmlSerializer.endTag(null, "student");
}
xmlSerializer.endTag(null, "students");
xmlSerializer.endDocument();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // pull读取xml文件
public void pullReaderXML() {
List<Student> listData = null;
Student student = null; try {
FileInputStream fileInputStream = new FileInputStream(
new File("data/data/cn.lixyz.xmltest/files/studentForPull.xml"));
XmlPullParser xmlPullParser = Xml.newPullParser();
xmlPullParser.setInput(fileInputStream, "UTF-8"); int event = xmlPullParser.getEventType(); while (event != xmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
listData = new ArrayList<Student>();
break; case XmlPullParser.START_TAG:
if ("student".equals(xmlPullParser.getName())) {
student = new Student();
int id = Integer.parseInt(xmlPullParser.getAttributeValue(0));
student.setId(id);
} else if ("name".equals(xmlPullParser.getName())) {
String name = xmlPullParser.nextText();
student.setName(name);
} else if ("age".equals(xmlPullParser.getName())) {
int age = Integer.parseInt(xmlPullParser.nextText());
student.setAge(age);
}
break;
case XmlPullParser.END_TAG:
if ("student".equals(xmlPullParser.getName())) {
listData.add(student);
}
}
event = xmlPullParser.next();
}
for (Student s : listData) {
Log.d("TTTT", s.getId() + "->" + s.getName() + "->" + s.getAge());
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} // 模拟生成xml的数据
private List<Student> createStudents() {
List<Student> listData = new ArrayList<Student>();
listData = new ArrayList<Student>();
Student s1 = new Student();
s1.setId(11);
s1.setName("张三");
s1.setAge(10);
listData.add(s1);
Student s2 = new Student();
s2.setId(22);
s2.setName("李四");
s2.setAge(11);
listData.add(s2);
Student s3 = new Student();
s3.setId(33);
s3.setName("王五");
s3.setAge(12);
listData.add(s3);
Student s4 = new Student();
s4.setId(44);
s4.setName("赵六");
s4.setAge(13);
listData.add(s4);
Student s5 = new Student();
s5.setId(55);
s5.setName("刘七");
s5.setAge(14);
listData.add(s5);
return listData;
}
}

Student.java

package cn.lixyz.xmltest.model;

public class Student {

    private int id;
private String name;
private int age; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }

  

上一篇:Java sax、dom、pull解析xml


下一篇:嵌入式新闻早班车-第7期