Android上解析.plist配置文件的xml解析器

来源:互联网 发布:win7联想网络驱动下载 编辑:程序博客网 时间:2024/05/22 21:42

plist是苹果机制的xml文件

格式如下:

<plistversion="1.0">
<array>
        <dict>
                <key>countryDomain</key>
                <string>HK</string>
                <key>code</key>
                <string>852</string>
                <key>country</key>
                <string>Hong Kong</string>
        </dict>
        <dict>
                <key>countryDomain</key>
                <string>CN</string>
                <key>code</key>
                <string>86</string>
                <key>country</key>
                <string>China</string>
        </dict>
</array>
</plist>

我相信很多人都是在做iphone的软件转移到android平台去,要怎么解析呢?我找到了一个牛人写的辅助android下解析这个苹果机制的xml的工具类

import  java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import android.util.Log;
 
/**
 *解析苹果机制下的plist
 * @author chen_weihua
 */
public class PlistHandler extends DefaultHandler {  
 
   private LinkedList<Object> list = new LinkedList<Object>();   
   //是否为根标签
   private boolean isRootElement = false;  
   //标签开始
   private boolean keyElementBegin = false;
   //键
   private String key;  
   //值开始
   private boolean valueElementBegin = false;   
   //根对象
   private Object root;  
       
 
   @SuppressWarnings("unchecked")  
   public Map getMapResult() {  
        return (Map)root;  
   }  
    
   @SuppressWarnings("unchecked")  
   public List getArrayResult() {  
        return (List)root;  
   }  
    
   @SuppressWarnings("unchecked")  
   @Override  
   public void startElement(String uri, String localName, String qName,  
            Attributes attributes)throws SAXException {  
        if("plist".equals(localName)) {  
            isRootElement =true;  
        }  
        if("dict".equals(localName)) {  
            if(isRootElement) {  
                list.addFirst(new HashMap());  
                isRootElement = !isRootElement;  
            }else{  
                ArrayList parent = (ArrayList)list.get(0);  
                list.addFirst(new HashMap());  
                parent.add(list.get(0));  
            }  
        }  
 
          
 
        if("key".equals(localName)) {  
            keyElementBegin =true;  
        }  
 
        if("true".equals(localName)) {  
            HashMap parent = (HashMap)list.get(0);  
            parent.put(key,true);  
        }  
 
        if("false".equals(localName)) {  
            HashMap parent = (HashMap)list.get(0);  
            parent.put(key,false);  
 
        }  
 
        if("array".equals(localName)) {  
            if(isRootElement) {  
                ArrayList obj =new ArrayList();  
                list.addFirst(obj);  
                isRootElement = !isRootElement;  
            }else{  
                HashMap parent = (HashMap)list.get(0);  
                ArrayList obj =new ArrayList();  
                list.addFirst(obj);  
                parent.put(key, obj);  
            }  
 
        }  
 
        if("string".equals(localName)) {  
            valueElementBegin =true;  
        }  
 
   }  
 
   @SuppressWarnings("unchecked")  
   @Override  
   public void characters(char[] ch, intstart,intlength)
            throws SAXException {  
        if(length > 0) {  
            if(keyElementBegin) {  
                key =new String(ch, start, length);  
                Log.d("AR native","key:"+ key);  
 
            }  
 
            if(valueElementBegin) {           
               if(HashMap.class.equals(list.get(0).getClass())) {  
                    HashMap parent = (HashMap)list.get(0);  
                    String value =new String(ch, start, length);  
                    parent.put(key, value);  
                }else if(ArrayList.class.equals(list.get(0).getClass())) {  
                    ArrayList parent = (ArrayList)list.get(0);  
                    String value =new String(ch, start, length);  
                    parent.add(value);  
                }  
               //Log.d("AR native", "value:" + value);  
            }  
        }  
   }  
 
   
 
   @Override  
   public void endElement(String uri, String localName, String qName)  
            throws SAXException {  
        if("plist".equals(localName)) {  
            ;  
 
        }  
 
        if("key".equals(localName)) {  
            keyElementBegin =false;  
        }  
 
        if("string".equals(localName)) {  
            valueElementBegin =false;  
        }  
 
        if("array".equals(localName)) {  
            root = list.removeFirst();  
        }  
 
        if("dict".equals(localName)) {  
            root = list.removeFirst();  
        }  
 
   }  
 
}


有没有人用过呢?一起研究吧。
原工具类发布地址:http://www.pin5i.com/showtopic-android-.plist-xml.html
原创粉丝点击