android 解析plist文件 城市列表文件为例

前言

plist: 全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。Plist文件通常用于IOS开发中储存用户设置,也可以用于存储捆绑的信息。ios提供了该类文件的解析机制,android却没有,有时间需要统一两端的数据资源文件,就需要在android中进行解析

正文

下面以一个城市文件为例,进行解析

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>北京市</key>
		<dict>
			<key>0</key>
			<dict>
				<key>北京市</key>
				<array>
					<string>东城区</string>
					<string>西城区</string>
					<string>朝阳区</string>
					<string>丰台区</string>
					<string>石景山区</string>
					<string>海淀区</string>
					<string>门头沟区</string>
					<string>房山区</string>
					<string>通州区</string>
					<string>顺义区</string>
					<string>昌平区</string>
					<string>大兴区</string>
					<string>怀柔区</string>
					<string>平谷区</string>
					<string>密云县</string>
					<string>延庆县</string>
				</array>
			</dict>
		</dict>
	</dict>
	<dict>
	    ...
	</dict>
</array>
</plist>



解析

public void getcityinfo(final Context context) {
        PListXMLParser parser = new PListXMLParser();
        PListXMLHandler handler = new PListXMLHandler();
        parser.setHandler(handler);
        try {
            parser.parse(context.getAssets().open("city.plist"));        // city.plist是你要解析的文件,放在assets文件夹中
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        PList actualPList = ((PListXMLHandler) parser.getHandler()).getPlist();
        Array root = (Array) actualPList.getRootElement();
        for (int l = 0; l < root.size(); l++) {
            Dict dict = (Dict) root.get(l);
            Map<String, PListObject> provinceCities = dict.getConfigMap();
            Iterator iter = provinceCities.entrySet().iterator();
            List<List<String>> cityAreaNames = new ArrayList<>();
            List<String> cityNames = new ArrayList<>();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                Dict provinceRoot = (Dict) provinceCities.get(entry.getKey());
                Map<String, PListObject> province = provinceRoot.getConfigMap();                     
                provinces.add(entry.getKey().toString());    //添加省份到列表
                for (int i = 0; i < province.size(); i++) {
                    Dict cityRoot = (Dict) province.get(String.valueOf(i));
                    Map<String, PListObject> cities = cityRoot.getConfigMap();
                    Iterator iterCity = cities.entrySet().iterator();
                    while (iterCity.hasNext()) {
                        Map.Entry entryCity = (Map.Entry) iterCity.next();
                        Array districts = (Array) cities.get(entryCity.getKey());
                        String cityName = entryCity.getKey().toString();
                        cityNames.add(cityName);    //添加城市到列表
                        List<String> areaNames = new ArrayList<>();
                        for (int k = 0; k < districts.size(); k++) {
                            MyString district = (MyString) districts.get(k);   
                            areaNames.add(district.getValue());    //添加区域到列表
                        }
                        cityAreaNames.add(areaNames);
                    }
                }
                citys.add(cityNames);
            }
            areas.add(cityAreaNames);
        }
    }

内容解析获取后可通过Android-PickerView进行解析实现效果

下面是示例


文中所需资源 请移步至  资源下载


阅读更多
文章标签: android plist
个人分类: android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭