plist,即property list,是蘋果手機軟體開發中經常用到的隱藏檔格式,其實質就是XML檔案,但是它遵循一定的格式。比如包含一些指定的元素,如plist,array,string,key等。蘋果提供瞭解析該類檔案的機制,可惜在安卓開發工具中卻沒有提供。經常遇到的情況是,在開發蘋果和安卓手機軟體的時候,要統一資源檔,這個時候,我們可以採用GitHub中提供的開源android-plist-parser解決方案。
比如我們要解析一個全國省市區的一個plist檔案:
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"> <dict> <key>0</key> <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> </plist> <?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"><dict> <key>0</key> <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></plist>
那麼如何在android中解析呢?
一:在GitHub上下載android-plist-parser
二:解壓下載的包,並匯入android項目的類路徑裡。
三:解析該檔案,並將解析資訊列印在控制台。(因為該檔案不算太小,因此在新線程中運行,防止主線程卡死)
new Thread(new Runnable() { @Override public void run() { PListXMLParser parser = new PListXMLParser(); // 基於SAX的實現 PListXMLHandler handler = new PListXMLHandler(); parser.setHandler(handler); try { parser.parse(getAssets().open("area.plist")); // area.plist是你要解析的檔案,該檔案需放在assets檔案夾下 } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } PList actualPList = ((PListXMLHandler) parser.getHandler()).getPlist(); Dict root = (Dict) actualPList.getRootElement(); Map<String,PListObject> provinceCities = root.getConfigMap(); for(int i=0; i<provinceCities.keySet().size();i++) { Dict provinceRoot = (Dict) provinceCities.get(String.valueOf(i)); Map<String,PListObject> province = provinceRoot.getConfigMap(); String provinceName = province.keySet().iterator().next(); System.out.println("省份為:" + provinceName); // 列印省份 Dict cityRoot = (Dict) province.get(provinceName); Map<String,PListObject> cities = cityRoot.getConfigMap(); for(int j=0;j<cities.keySet().size();j++) { Dict city = (Dict) cities.get(String.valueOf(j)); String cityName = city.getConfigMap().keySet().iterator().next(); System.out.println("城市為:" + cityName); // 列印城市 Array districts = city.getConfigurationArray(cityName); for(int k=0;k<districts.size();k++) { com.longevitysoft.android.xml.plist.domain.String district = (com.longevitysoft.android.xml.plist.domain.String) districts.get(k); System.out.println("地區為:" + district.getValue()); // 列印地區 } } } } }).start(); new Thread(new Runnable() { @Override public void run() { PListXMLParser parser = new PListXMLParser(); // 基於SAX的實現 PListXMLHandler handler = new PListXMLHandler(); parser.setHandler(handler); try { parser.parse(getAssets().open("area.plist")); // area.plist是你要解析的檔案,該檔案需放在assets檔案夾下 } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } PList actualPList = ((PListXMLHandler) parser.getHandler()).getPlist(); Dict root = (Dict) actualPList.getRootElement(); Map<String,PListObject> provinceCities = root.getConfigMap(); for(int i=0; i<provinceCities.keySet().size();i++) { Dict provinceRooSystem.out.println("省份為:" + provinceName); // 列印省份 Dict cityRoot = (Dict) province.get(provinceName); Map<String,PListObject> cities = cityRoot.getConfigMap(); for(int j=0;j<cities.keySet().size();j++) { Dict city = (Dict) cities.get(String.valueOf(j)); String cityName = city.getConfigMap().keySet().iterator().next(); System.out.println("城市為:" + cityName); // 列印城市 Array districts = city.getConfigurationArray(cityName); for(int k=0;k<districts.size();k++) { com.longevitysoft.android.xml.plist.domain.String district = (com.longevitysoft.android.xml.plist.domain.String) districts.get(k); System.out.println("地區為:" + district.getValue()); // 列印地區 } } } } }).start(); t = (Dict) provinceCities.get(String.valueOf(i)); Map<String,PListObject> province = provinceRoot.getConfigMap(); String provinceName = province.keySet().iterator().next();