pull解析XML文件

核心代码

package com.example.createxml;

import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Xmlpull extends ActionBarActivity {

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

    public void click(View v) {
        List<Message> list=null;
        Message msg=null;
        File file = new File(Environment.getExternalStorageDirectory(), "message.xml");
        try {
            FileInputStream fis = new FileInputStream(file);
//          1.得到解析器对象
            XmlPullParser xp = Xml.newPullParser();
//          2.初始化xp对象
            xp.setInput(fis, "utf-8");
            int type = xp.getEventType();
//          3.开始解析,获取当前结点的事件类型
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if("messages".equals(xp.getName())){
                            list=new ArrayList<>();
                        }else if("message".equals(xp.getName())){
                            msg=new Message();
                        }else if("address".equals(xp.getName())){
                            //获取当前结点的下一个节点的文本然后把指针移动到当前结点的结束结点
                            String address=xp.nextText();
                            msg.setAddress(address);

                        }else if("body".equals(xp.getName())){
                            //获取当前结点的下一个节点的文本然后把指针移动到当前结点的结束结点
                            String body=xp.nextText();
                            msg.setBody(body);

                        }else if("type".equals(xp.getName())){
                            //获取当前结点的下一个节点的文本然后把指针移动到当前结点的结束结点
                            String type1=xp.nextText();
                            msg.setType(type1);

                        }else if("date".equals(xp.getName())){
                            //获取当前结点的下一个节点的文本然后把指针移动到当前结点的结束结点
                            String date=xp.nextText();
                            msg.setDate(date);

                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("message".equals(xp.getName())){
                            list.add(msg);
                        }
                        break;
                }

                type=xp.next();
//                type = xp.getEventType();
            }
            Log.e("gjj",list.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.createxml.Xmlpull">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="解析XML文件"
        android:onClick="click"/>

</RelativeLayout>

message.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<messages>
    <message>
        <address>苗戴0</address>
        <date>1451094799432</date>
        <type>1</type>
        <body>我是body0</body>
    </message>
    <message>
        <address>苗戴1</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body1</body>
    </message>
    <message>
        <address>苗戴2</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body2</body>
    </message>
    <message>
        <address>苗戴3</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body3</body>
    </message>
    <message>
        <address>苗戴4</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body4</body>
    </message>
    <message>
        <address>苗戴5</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body5</body>
    </message>
    <message>
        <address>苗戴6</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body6</body>
    </message>
    <message>
        <address>苗戴7</address>
        <date>1451094799433</date>
        <type>1</type>
        <body>我是body7</body>
    </message>
    <message>
        <address>苗戴8</address>
        <date>1451094799434</date>
        <type>1</type>
        <body>我是body8</body>
    </message>
    <message>
        <address>苗戴9</address>
        <date>1451094799434</date>
        <type>1</type>
        <body>我是body9</body>
    </message>
</messages>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值