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>

 

posted @ 2019-06-16 00:44  TPrime.A  阅读(643)  评论(0编辑  收藏  举报