Android --pull解析XML,获取省市区的例子

<?xml version="1.0" encoding="utf-8"?>
<data>
    <region
        name="台北市"
        id="1"
        lat="25.091075"
        lng="121.55983449999997"
        order="1">
        <section
            id="1"
            lat="25.0421407"
            lng="121.5198716"
            order="7">中正區</section>
        <section
            id="2"
            lat="25.0627243"
            lng="121.5113064"
            order="10">大同區</section>
        <section
            id="3"
            lat="25.0792018"
            lng="121.5427093"
            order="1">中山區</section>
         
    </region>
    <region
        name="新北市"
        id="3"
        lat="25.016205"
        lng="121.463255"
        order="2">
        <section
            id="20"
            lat="25.1676024"
            lng="121.6397184"
            order="22">萬里區</section>
        <section
            id="21"
            lat="25.2366076"
            lng="121.6169002"
            order="21">金山區</section>
        <section
            id="26"
            lat="25.0130994"
            lng="121.4627576"
            order="1">板橋區</section>
       
        
    </region>
    <region
        name="基隆市"
        id="2"
        lat="25.1089809"
        lng="121.70814540000003"
        order="9">
        <section
            id="13"
            lat="25.1187161"
            lng="121.7451927"
            order="4">仁愛區</section>
        <section
            id="14"
            lat="25.1284336"
            lng="121.7822278"
            order="3">信義區</section>
        <section
            id="15"
            lat="25.1489988"
            lng="121.7736823"
            order="1">中正區</section>
         
    </region>
</data>

举个这样的例子,这是一个XML的部分数据,现在需要解析

**
 * 城市解析XML
 * Created by yangbin on 2019/4/1.
 */
public class CityXmlPraserUtils {

    public static List<FirstEntity> parserLocation(Context context) {
        List<FirstEntity> cityList = new ArrayList<>();
        try {
            InputStream inStream = context.getResources().openRawResource(R.raw.location);
            XmlPullParser pullParser = Xml.newPullParser();
            pullParser.setInput(inStream, "UTF-8");
            int event = pullParser.getEventType();// 觸發第一個事件
            cityList.add(new FirstEntity("0", "不限", "", "", ""));
            List<SecondEntity> districtList = new ArrayList<>();
            FirstEntity tempCity;
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        if ("region".equals(pullParser.getName())) {
                            String regionName = pullParser.getAttributeValue(0);
                            String regionId = pullParser.getAttributeValue(1);
                            String lat = pullParser.getAttributeValue(2);
                            String lng = pullParser.getAttributeValue(3);
                            String orderId = pullParser.getAttributeValue(4);
                            tempCity = new FirstEntity(regionId, regionName, lat, lng, orderId);
                            districtList = new ArrayList<>();
                            districtList.add(new SecondEntity("0", "不限", "", "", ""));
                            tempCity.setSecondEntityList(districtList);
                            cityList.add(tempCity);
                        }
                        if ("section".equals(pullParser.getName())) {
                            String sectionId = pullParser.getAttributeValue(0);
                            String lat = pullParser.getAttributeValue(1);
                            String lng = pullParser.getAttributeValue(2);
                            String orderId = pullParser.getAttributeValue(3);

                            String sectionName = pullParser.nextText();

                            SecondEntity district = new SecondEntity(sectionId, sectionName, lat, lng, orderId);
                            districtList.add(district);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        break;
                }
                event = pullParser.next();
            }
        } catch (Exception e) {
        }
        return cityList;
    }
}

 

最后的效果图

写这个帖子是记录下,解析这块,说实话,我居然忘记怎么解析了,还是后面百度别人的文章,跟看同事的代码,所以基础啊 不牢固

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/yangbin0513/article/details/90202357