android解析plist文件

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" "https://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" "https://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();
    

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *