android开发:xml解析读取的方法XmlResourceParser、XmlPullParser

  • XmlResourceParser

先来说说XmlResourceParser,看名字应该就是用来读取APK内置的XML资源来使用的,解析的方法跟XmlPullParser是一样的,下面来看演示代码:

public Object parserXml(XmlResourceParser xml){

    ArrayList<areaInfo> _list = new ArrayList<>();//三维数组对象
    ArrayList<areaInfo> _city = new ArrayList<>();      //城市二维数组对象

    areaInfo province = new areaInfo();     //省份对象
    areaInfo city = new areaInfo();     //城市对象

    try {

        while (xml.getEventType() != XmlResourceParser.END_DOCUMENT){  //XML开始解析

            String name = null;

            if(xml.getEventType() == XmlResourceParser.START_TAG){      //标签开头

                name = xml.getName();//标签的名字

                if (name.trim().toLowerCase().equals("province")){      //省份查询
                    province = new areaInfo();
                    province.id = Integer.valueOf(xml.getAttributeValue(0));//id
                    province.pid = Integer.valueOf(xml.getAttributeValue(1));//pid
                    province.name = xml.getAttributeValue(2); //name
                    province.extend = new ArrayList<>();

                }else if (name.trim().toLowerCase().equals("city")){   //地级市
                    city = new areaInfo();
                    city.id = -1;
                    city.pid = Integer.valueOf(xml.getAttributeValue(0));//CityID
                    city.name = xml.getAttributeValue(1); //City
                    city.extend = new ArrayList<>();

                }else if (name.trim().toLowerCase().equals("piecearea")){   //区县
                    areaInfo area = new areaInfo();
                    area.id = -1;
                    area.pid = Integer.valueOf(xml.getAttributeValue(0));//PieceareaID
                    area.name = xml.getAttributeValue(1); //Piecearea

                    city.extend.add(area);//把区县加入地级市
                }

            }else if (xml.getEventType() == XmlResourceParser.END_TAG){     //标签结束
                name = xml.getName();

                if(name.toLowerCase().trim().equals("province")){   //省份标签结束,把城市加入到集合中
                    province.extend = _city;
                    _city = new ArrayList<>();
                    _list.add(province);

                }else if (name.toLowerCase().trim().equals("city")){    //城市标签结束,把城市加入_city集合
                    _city.add(city);
                }

            }else if (xml.getEventType() == XmlResourceParser.TEXT){
                //Log.d(TAG,"TEXT:"+xml.getText());
            }

            xml.next();//读取下一个标签
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }


    return _list;
}

我这边解析的是一个area.xml文件,这个文件是一个三级集合的对象,下面来看一下xml的截图:

QQ截图20160712104657
areaInfo是一个简单的地区信息类,下面贴出这个类的源代码:

/**
 * 一个简单的区域信息类,用来存放城市ID已经名字
 */
class areaInfo {
    public int id = -1;
    public int pid;
    public String name;
    public ArrayList<areaInfo> extend;

    public areaInfo(){

    }

    public int getId(){
        return id;
    }

    public int getPid(){
        return pid;
    }

    public String getName(){
        return name;
    }

    public String toString(){
        return "id:"+id+" pid:"+pid+" name:"+name;
    }
}

这样就可以解析到了,关于xml的文件下载地址,可以戳这里

  • XmlPullParser

XmlPullParser的解析方法也很简单,下面看一下代码:

public Object pullXml(InputStream is,String encod){


    XmlPullParser xml = Xml.newPullParser();    //创建XmlPullParser对象

    ArrayList<areaInfo> _list = new ArrayList<>();  //创建三维数组集合对象
    ArrayList<areaInfo> _city = new ArrayList<>();  //创建一个城市列表对象,二维数组

    areaInfo province = new areaInfo(); //省份储存对象
    areaInfo city = new areaInfo();     //城市存储对象

    try {

        xml.setInput(is,encod);     //设置输入的xml内容

        while (xml.getEventType() != XmlPullParser.END_DOCUMENT){
            String name = null;


            if(xml.getEventType() == XmlPullParser.START_TAG){      //标签解析开始代码

                name = xml.getName();//标签的名字

                if (name.trim().toLowerCase().equals("province")){      //省份标签
                    province = new areaInfo();
                    province.id = Integer.valueOf(xml.getAttributeValue(0));//id
                    province.pid = Integer.valueOf(xml.getAttributeValue(1));//pid
                    province.name = xml.getAttributeValue(2); //name
                    province.extend = new ArrayList<>();

                    //Log.d(TAG,"START_TAG:"+xml.getAttributeValue(0)+" "+xml.getAttributeValue(1)+" "+xml.getAttributeValue(2));

                }else if (name.trim().toLowerCase().equals("city")){   //城市标签
                    city = new areaInfo();
                    city.id = -1;
                    city.pid = Integer.valueOf(xml.getAttributeValue(0));//CityID
                    city.name = xml.getAttributeValue(1); //City
                    city.extend = new ArrayList<>();

                }else if (name.trim().toLowerCase().equals("piecearea")){   //区县标签
                    areaInfo area = new areaInfo();
                    area.id = -1;
                    area.pid = Integer.valueOf(xml.getAttributeValue(0));//PieceareaID
                    area.name = xml.getAttributeValue(1); //Piecearea

                    city.extend.add(area);//把区县加入城市对象
                }

            }else if (xml.getEventType() == XmlPullParser.END_TAG){

                name = xml.getName();

                if(name.toLowerCase().trim().equals("province")){       //省份标签结束,把城市加入到集合中
                    province.extend = _city;
                    _city = new ArrayList<>();
                    _list.add(province);//加入集合

                }else if (name.toLowerCase().trim().equals("city")){    //城市标签结束,把城市加入_city集合
                    _city.add(city);
                }


            }else if (xml.getEventType() == XmlPullParser.TEXT){
                //Log.d(TAG,"TEXT:"+xml.getText());
            }

            //读取下一个标签
            xml.next();

        }

    } catch (XmlPullParserException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return _list;
}

前台的操作代码:

 @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.but4:
                Spinner spinner = (Spinner)findViewById(R.id.spinner);

                xmlUtils xml = new xmlUtils(mContext);
                final ArrayList<areaInfo> list = (ArrayList<areaInfo>) xml.parserXml(mContext.getResources().getXml(R.xml.area));

                SpinnerAdapter adapter = new ArrayAdapter<areaInfo>(mContext,R.layout.spinneritem,list);

                int pro_1 = 0;
                int city_1 = 0;
                int area_1 = 0;
                if(list != null){
                    for (areaInfo province : list){
                        Log.d(TAG,"A:"+province.toString());
                        pro_1++;

//                        for (areaInfo city2 : province.extend){
//                            Log.d(TAG,"B:"+city2.toString());
//                            city_1++;
//                            for (areaInfo area2 : city2.extend){
//                                area_1++;
//                                Log.d(TAG,"C:"+area2.toString());
//                            }
//                        }

                    }
                }
                spinner.setAdapter(adapter);
                Log.i(TAG,"省:"+pro_1+"个 地级市:"+city_1+"个 区县:"+area_1+"个");


                //添加监听事件
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        Log.i(TAG,"i:"+i+"  l:"+l+" ");
                        areaInfo info = list.get(i);

                        Log.i(TAG,"省份:"+info.name+" pid:"+info.pid+" id:"+info.id);

                        for (areaInfo test: info.extend){
                            Log.d(TAG,"Other A:"+test.toString());
                            for (areaInfo test2: test.extend){
                                Log.d(TAG,"Other B:"+test2.toString());
                            }
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });







                break;
            case R.id.but5:
                InputStream in = null;
                try {

                    in = new FileInputStream(new File(Environment.getExternalStorageDirectory()+"/area.xml"));

                    xmlUtils xml2 = new xmlUtils(mContext);
                    ArrayList<areaInfo> list2 = (ArrayList<areaInfo>) xml2.pullXml(in,"utf-8");

                    int pro_2 = 0;
                    int city_2 = 0;
                    int area_2 = 0;

                    if(list2 != null){
                        for (areaInfo province : list2){
                            Log.d(TAG,"A:"+province.toString());
                            pro_2++;

                            for (areaInfo city2 : province.extend){
                                    Log.d(TAG,"B:"+city2.toString());
                                    city_2++;
    //                            for (areaInfo area2 : city2.extend){
    //                                area_2++;
    //                                Log.d(TAG,"C:"+area2.toString());
    //                            }
                                }

                        }
                    }
                    Log.i(TAG,"省:"+pro_2+"个 地级市:"+city_2+"个 区县:"+area_2+"个");

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


                break;
            default:
                Log.i(TAG,"Nothing...");
                break;
        }

    }

 

效果截图:

QQ截图20160712104532 QQ截图20160712104539

QQ截图20160712104551

 

 

Leave a Comment

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |