Android Plist文件解析

来源:互联网 发布:商业网站域名后缀 编辑:程序博客网 时间:2024/05/06 03:36

1.在GitHub上下载android-plist-parser(点击打开链接)

2. 解析主要代码如下:

private void parsePListObject(PListObject pListObject) {if (pListObject instanceof Dict) {Dict dict = (Dict) pListObject;parseDict(dict);}else if ( pListObject instanceof Array) {Array array = (Array) pListObject;parseArray(array);}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.Date) {Date date = (Date) pListObject;parseDate(date);}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.String) {com.longevitysoft.android.xml.plist.domain.String myString = (com.longevitysoft.android.xml.plist.domain.String) pListObject;System.out.println("String====" + myString.getValue());sb.append(  myString.getValue() + "\n");}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.Integer) {com.longevitysoft.android.xml.plist.domain.Integer myInteger = (com.longevitysoft.android.xml.plist.domain.Integer) pListObject;System.out.println("Integer====" + myInteger.getValue());sb.append( myInteger.getValue() + "\n");}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.Real) {Real myReal = (Real) pListObject;System.out.println("Real====" + myReal.getValue());sb.append(  myReal.getValue() + "\n");}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.False) {False myFalse = (False) pListObject;System.out.println("False====" + myFalse.getValue());sb.append( myFalse.getValue() + "\n");}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.True) {True myTrue = (True) pListObject;System.out.println("True====" + myTrue.getValue());sb.append(  myTrue.getValue() + "\n");}else if (pListObject instanceof com.longevitysoft.android.xml.plist.domain.Data) {Data data = (Data) pListObject;System.out.println("Data===="+data.getValue());sb.append(data.getValue() + "\n");}}private void parseDate(Date date) {java.util.Date value = date.getValue();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format = sdf.format(value);System.out.println("Date===="+format);sb.append(format + "\n");}/** * 解析Array类型 * @param noteArray */private void parseArray(Array noteArray) {for(int k=0;k<noteArray.size();k++) {PListObject pListObject = noteArray.get(k);parsePListObject(pListObject);}}/** * 解析Dict类型 * @param dict */private void parseDict(Dict dict) {Map<String,PListObject> notes = dict.getConfigMap();Set<String> keySet = notes.keySet();Iterator<String> iterator = keySet.iterator();while(iterator.hasNext()){String tempKey = iterator.next();PListObject pListObject = notes.get(tempKey);sb.append(tempKey + " : ");//文件中的节点名称parsePListObject(pListObject);}}
//调用代码如下:PListXMLParser parser = new PListXMLParser(); // 基于SAX的实现PListXMLHandler handler = new PListXMLHandler();parser.setHandler(handler);try {// waiter.plist是你要解析的文件,该文件需放在assets文件夹下parser.parse(getAssets().open("waiter.plist")); } catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}PList actualPList = ((PListXMLHandler) parser.getHandler()).getPlist();PListObject rootElement = actualPList.getRootElement();parsePListObject(rootElement);


0 0