1. 程式人生 > >Android應用開發--使用XmlPullParser解析XML檔案

Android應用開發--使用XmlPullParser解析XML檔案

使用Android SDK自帶的XmlPullParser類可以很方便的解析XML檔案

參考連線:https://developer.android.google.cn/reference/org/xmlpull/v1/XmlPullParser?hl=en

準備一個XML檔案:

<?xml version="1.0" encoding="utf-8"?>
<Params name="demo">
    <Param name="com.sina.weibo"/>
</Params> 

新建一個HelloWorld程式,示例程式碼如下:

package com.tprime.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Xml;
import android.view.View;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String TAG = "TAG_XML_PULL_PARSER";
    private String configPath = "/sdcard/blacklist.xml";

    private List<String> filterList = new ArrayList<String>();
    private List<String> testNameList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        testNameList.add("com.sina.weibo");
        testNameList.add("com.sina.weibo.MainActivity");
        testNameList.add("com.sina");
        testNameList.add("com");
        testNameList.add("");
        testNameList.add("/com.sina");
        testNameList.add("com.*sina");
        testNameList.add(null);
    }

    public void inFilterList(View v){
        StringBuffer sb = new StringBuffer();
        for(String name: testNameList) {
            sb.append(name + "\t");
            sb.append(filterList.contains(name));
            sb.append("\n");
        }
        Log.d(TAG, sb.toString());
    }

    public void parseConfigXml(View v) {
        Log.d(TAG, "parseConfigXml +++++++++++++++++++++++++++");

        FileReader fileReader = null;
        try {
            File file = new File(configPath);
            fileReader = new FileReader(file);

            // XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            // XmlPullParser xmlParser = factory.newPullParser();
            XmlPullParser xmlParser = Xml.newPullParser();
            xmlParser.setInput(fileReader);
            int eventType = xmlParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        Log.d(TAG, "OUTER if Param: " + xmlParser.getAttributeValue(0));
                        if (xmlParser.getName().equals("Param")) {
                            Log.d(TAG, "INNER if Param: " + xmlParser.getAttributeValue(0));
                            if (xmlParser.getAttributeName(0).equals("name")) {
                                filterList.add(xmlParser.getAttributeValue(0));
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                    default:
                        break;
                }
                eventType = xmlParser.next();
            }
        } catch (XmlPullParserException e) {
            Log.e(TAG, "ERROR 01: " + e.getStackTrace().toString());
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "ERROR 02: " + e.getStackTrace().toString());
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileReader != null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Log.d(TAG, "parseConfigXml ---------------------------");
    }
}

對應的xml而已很簡單,就兩個按鈕,程式碼如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:text="開始解析"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="parseConfigXml"/>

    <Button
        android:text="是否在名單中"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="inFilterList"/>
</android.support.v7.widget.LinearLayoutCompat>