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

plist是苹果机制的xml文件

格式如下:

< plist version = "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, int start, int length)
             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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值