android解析plist檔案

來源:互聯網
上載者:User

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();    

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.