XML解析三种方式
DOM
通用性强,它会将XML文件的所有内容读取到内存中,然后允许您使用DOM API遍历XML树、检索所需的数据;
简单直观,但需要将文档读取到内存,并不太适合移动设备;
SAX
SAX是一个解析速度快并且占用内存少的xml解析器;
采用事件驱动,它并不需要解析整个文档;
实现:继承DefaultHandler,覆写startElement、endElement、characters等方法;
PULL
Android自带的XML解析器,和SAX基本类似,也是事件驱动,不同的是PULL事件返回的是数值型;推荐使用。
PULL解析XML核心代码
- XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
- parser.setInput(fileInputStream, “utf-8”);//设置数据源编码
- int eventCode = parser.getEventType();//获取事件类型
- while(eventCode != XmlPullParser.END_DOCUMENT) {
- switch (eventCode){
- case XmlPullParser.START_DOCUMENT: //开始读取XML文档
- //实例化集合类
- break;
- case XmlPullParser.START_TAG://开始读取某个标签
- if("person".equals(parser.getName())) {
- //通过getName判断读到哪个标签,然后通过nextText()获取文本节点值,或通过getAttributeValue(i)获取属性节点值
- }
- break;
- case XmlPullParser.END_TAG://读完一个Person,可以将其添加到集合类中
- break;
- }
- parser.next();
- }
PULL解析XML示例方法
Java方法
- public static List<Person> readXML(InputStream inStream) {
- XmlPullParser parser = Xml.newPullParser();
- try {
- parser.setInput(inStream, "UTF-8");// 设置数据源编码
- int eventType = parser.getEventType();// 获取事件类型
- Person currentPerson = null;
- List<Person> persons = null;
- while (eventType != XmlPullParser.END_DOCUMENT) {
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
- persons = new ArrayList<Person>();// 实例化集合类
- break;
- case XmlPullParser.START_TAG://开始读取某个标签
- //通过getName判断读到哪个标签,然后通过nextText()获取文本节点值,或通过getAttributeValue(i)获取属性节点值
- String name = parser.getName();
- if (name.equalsIgnoreCase("person")) {
- currentPerson = new Person();
- currentPerson.setId(new Integer(parser.getAttributeValue(null, "id")));
- } else if (currentPerson != null) {
- if (name.equalsIgnoreCase("name")) {
- currentPerson.setName(parser.nextText());// 如果后面是Text元素,即返回它的值
- } else if (name.equalsIgnoreCase("age")) {
- currentPerson.setAge(new Short(parser.nextText()));
- }
- }
- break;
- case XmlPullParser.END_TAG:// 结束元素事件
- //读完一个Person,可以将其添加到集合类中
- if (parser.getName().equalsIgnoreCase("person")&& currentPerson != null) {
- persons.add(currentPerson);
- currentPerson = null;
- }
- break;
- }
- eventType = parser.next();
- }
- inStream.close();
- return persons;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
应用
XML文件
- <?xml version="1.0" encoding="UTF-8"?>
- <persons>
- <person id="23">
- <name>孙洋洋</name>
- <age>30</age>
- </person>
- <person id="20">
- <name>孙彬彬</name>
- <age>25</age>
- </person>
- </persons>
- package com.app.data;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserException;
- import org.xmlpull.v1.XmlPullParserFactory;
- import com.app.other.Person;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Xml;
- import android.widget.TextView;
- public class XMLActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_xml);
- StringBuilder sBuilder = new StringBuilder();
- File xmlFile = new File("/mnt/sdcard/App/person.xml");
- try {
- FileInputStream inputStream = new FileInputStream(xmlFile);
- // URL url = new URL("http://10.0.2.2:8888/android/person.xml");
- // HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
- // InputStream inputStream = urlConnection.getInputStream();
- List<Person> list = readXML(inputStream);
- for (int i = 0; i < list.size(); i++) {
- Person person = list.get(i);
- sBuilder.append("第-" + i + "-条记录:\n");
- sBuilder.append("getName--" + person.getName() + "\n");
- sBuilder.append("getAge--" + person.getAge() + "\n");
- sBuilder.append("getId--" + person.getId() + "\n");
- }
- inputStream.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- TextView textView = (TextView)findViewById(R.id.textView2);
- textView.setText(sBuilder);
- }
- public static List<Person> readXML(InputStream inStream) {
- XmlPullParser parser = Xml.newPullParser();
- try {
- parser.setInput(inStream, "UTF-8");// 设置数据源编码
- int eventType = parser.getEventType();// 获取事件类型
- Person currentPerson = null;
- List<Person> persons = null;
- while (eventType != XmlPullParser.END_DOCUMENT) {
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
- persons = new ArrayList<Person>();// 实例化集合类
- break;
- case XmlPullParser.START_TAG://开始读取某个标签
- //通过getName判断读到哪个标签,然后通过nextText()获取文本节点值,或通过getAttributeValue(i)获取属性节点值
- String name = parser.getName();
- if (name.equalsIgnoreCase("person")) {
- currentPerson = new Person();
- currentPerson.setId(new Integer(parser.getAttributeValue(null, "id")));
- } else if (currentPerson != null) {
- if (name.equalsIgnoreCase("name")) {
- currentPerson.setName(parser.nextText());// 如果后面是Text元素,即返回它的值
- } else if (name.equalsIgnoreCase("age")) {
- currentPerson.setAge(new Short(parser.nextText()));
- }
- }
- break;
- case XmlPullParser.END_TAG:// 结束元素事件
- //读完一个Person,可以将其添加到集合类中
- if (parser.getName().equalsIgnoreCase("person")&& currentPerson != null) {
- persons.add(currentPerson);
- currentPerson = null;
- }
- break;
- }
- eventType = parser.next();
- }
- inStream.close();
- return persons;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="15dp" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="XML文件读取"
- android:layout_gravity="center_horizontal" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
![](https://i-blog.csdnimg.cn/blog_migrate/e552da042334029daabe9c549cebf28e.jpeg)
执行结果:
![](https://i-blog.csdnimg.cn/blog_migrate/303cfdfc3125cf954e853d393cf0c318.jpeg)