当前位置:操作系统 > 安卓/Android >>

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

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,