Android使用Pull方式解析XML

本文介绍了Android中推荐的XML解析方式——Pull解析器。它采用事件驱动,通过处理开始和结束事件来解析XML。文章详细讲解了常用事件类型(如START_DOCUMENT, START_TAG等)和方法(如getEventType(), getName()等),并提供了代码示例展示如何解析XML文件。" 130478368,18090643,Qt编程:QDir与QFileInfo实现文件系统操作,"['Qt开发', 'C++', '文件系统']
摘要由CSDN通过智能技术生成

简单介绍:

在Android应用开发中Pull方式解析XML是最被推荐的一种,Android系统内置了Pull解析器用来解析XML文件,它是采用事件驱动来完成XML解析的,只需要处理开始和结束事件,通常使用switch语句根据事件的类型匹配不同的处理方式。

最常使用的事件类型:
1. START_DOCUMENT:开始解析(只执行一次)
2. START_TAG:开始元素
3. TEXT:解析文本
4. END_TAG:结束元素
5. END_DOCUMENT:结束解析(只执行一次)

最常使用的方法:
1. getEventType():触发并获取第一个事件类型(START_DOCUMENT)
2. getName():获取元素名称
3. nextText():获取当前元素的值
4. next():触发下一个事件
5. getAttributeValue():获取当前元素的属性值(参数为索引,还有一个重载方法,是通过指定命名空间获取的)

用代码实现一下:
在assets目录中创建一个XML文件

<?xml version="1.0" encoding="utf-8"?>
<persons>
    <person>
        <id>1</id>
        <name>zhangsan</name>
        <age>18</age>
    </person>
    <person>
        <id>2</id>
        <name>lisi</name>
        <age>19</age>
    </person>
</persons>
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivityInfo";
    private Button btnPull;

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

        initView();
    }

    /**
     * 初始化UI
     */
    private void initView() {
        btnPull = (Button) findViewById(R.id.btn_pull);

        btnPull.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream inputStream = getXmlData();
                try {
                    parseXMLWithPull(inputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 从assets目录中获取xml数据
     *
     * @return xml数据
     */
    public InputStream getXmlData() {
        //获取assets管理者
        AssetManager assetManager = getAssets();
        InputStream inputStream = null;
        try {
            //打开文件获取输入流
            //inputStream输入流不能在此关闭,因为它还要作为其它方法的参数
            inputStream = assetManager.open("persons.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    /**
     * 使用Pull方式解析xml
     *
     * @param xmlData xml数据
     */
    private void parseXMLWithPull(InputStream xmlData) throws IOException {
        try {
            //创建一个xml解析的工厂
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            //获取xml解析类的实例
            XmlPullParser parser = factory.newPullParser();
            //设置需要解析数据
            parser.setInput(xmlData, "UTF-8");
            //获取解析事件
            int eventType = parser.getEventType();

            switch (parser.getEventType()) {
                case 0:
                    Log.i(TAG, "START_DOCUMENT");
                    break;
                case 1:
                    Log.i(TAG, "END_DOCUMENT");
                    break;
                case 2:
                    Log.i(TAG, "START_TAG");
                    break;
                case 3:
                    Log.i(TAG, "END_TAG");
                    break;
                case 4:
                    Log.i(TAG, "TEXT");
                    break;
            }

            String id = "";
            String name = "";
            String age = "";
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String nodeName = parser.getName();
                Log.i(TAG, "nodeName = " + nodeName);
                switch (eventType) {
                    //开始解析某个结点
                    case XmlPullParser.START_TAG:
                        if ("id".equals(nodeName)) {
                            id = parser.nextText();
                        } else if ("name".equals(nodeName)) {
                            name = parser.nextText();
                        } else if ("age".equals(nodeName)) {
                            age = parser.nextText();
                        }
                        break;

                    //完成解析某个结点
                    case XmlPullParser.END_TAG:
                        if ("person".equals(nodeName)) {
                            Log.i(TAG, "id = " + id);
                            Log.i(TAG, "name = " + name);
                            Log.i(TAG, "age = " + age);
                        }
                        break;
                }
                //获取下一个解析事件
                eventType = parser.next();
                switch (parser.getEventType()) {
                    case 1:
                        Log.i(TAG, "END_DOCUMENT");
                        break;
                    case 2:
                        Log.i(TAG, "START_TAG");
                        break;
                    case 3:
                        Log.i(TAG, "END_TAG");
                        break;
                    case 4:
                        Log.i(TAG, "TEXT");
                        break;
                }
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //最后在关闭输入流
        xmlData.close();
    }
}

打印信息:

03-20 16:09:11.047 7554-7554/? I/MainActivityInfo: START_DOCUMENT
03-20 16:09:11.047 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = persons
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.048 7554-7554/? I/MainActivityInfo: nodeName = id
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.049 7554-7554/? I/MainActivityInfo: nodeName = name
03-20 16:09:11.050 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.050 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.050 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = age
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: END_TAG
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: id = 1
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: name = zhangsan
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: age = 18
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.051 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = id
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = name
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: START_TAG
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = age
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.052 7554-7554/? I/MainActivityInfo: END_TAG
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: nodeName = person
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: id = 2
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: name = lisi
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: age = 19
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: TEXT
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: nodeName = null
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: END_TAG
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: nodeName = persons
03-20 16:09:11.053 7554-7554/? I/MainActivityInfo: END_DOCUMENT
从上面的输出信息中可以得出程序执行的大致过程:自上而下,逐行解析。先从persons开始,然后是person,再然后是id,就这样逐行往下进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值