Android解析Plist文件 [英] Android parse Plist file

查看:374
本文介绍了Android解析Plist文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试图解析包含dict数组的plist文件。我尝试使用 xmlwise 执行此操作。 plistfile的内容是这里

Hello Im trying to parse an plist file that contains array of dict's. Im trying to do this using xmlwise. The content of the plistfile is here

到目前为止我只有这个在我的activity和im获取plistfile的内容,但是如何将内容解析为arraylist?

So far I only have this in my activity and im getting the content of the plistfile, but how to parse the content into an arraylist?

Map<String, Object> properties = null;
try {
    InputStream inputStream = getResources().openRawResource(R.raw.first_5);
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        properties = Plist.fromXml(sb.toString());
        // TODO do something with the object here
        Log.v("--", properties.values() + " " + properties.size());
        Log.v("--", "OB "+properties.get());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        br.close();
    }
}


推荐答案

快速题。 ArrayList的内容应该是什么?我想知道你是否在提及 Map< String,Object>中的 Object 列表属性 map那么为什么你只能从地图中获取值

Quick question. What should be the content of the ArrayList? I was wondering if you are mentioning about a list of Object in you Map<String, Object> properties map then why cant you just get the values from the map as

Map<String, Object> properties = new HashMap<String, Object>();
List<Object> plist = new ArrayList<Object>(properties.values());

除了检查你的plist之外,结构就像一个Dict根元素和一个Dicts列表。我假设您需要将此作为列表。如果是这样,请考虑使用longevitysoft的 Android PList解析器。这很简单,也是开源的。它基本上是一个SAXParser解析 Apple PList

Apart from that checking your plist the structure is like a Dict root element and a list of Dicts in it. I assume you need to get this as a list. If so consider using Android PList parser by longevitysoft. This is simple and opensource. Its basically a SAXParser parsing Apple PList.

然后,您可以遍历此数组并获取适当的对象。在你的xml中它和Dict对象的数组,所以你可以做这样的事情

You can then iterate through this array and get approriate object. In your xml its and array of Dict object, so you could do something like this

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.longevitysoft.android.xml.plist.PListXMLHandler;
import com.longevitysoft.android.xml.plist.PListXMLParser;
import com.longevitysoft.android.xml.plist.domain.Array;
import com.longevitysoft.android.xml.plist.domain.Dict;
import com.longevitysoft.android.xml.plist.domain.PList;
import com.longevitysoft.android.xml.plist.domain.PListObject;

public class PlistReader {

    public static void main(String[] args) throws Exception {
        PListXMLParser parser = new PListXMLParser();
        PListXMLHandler handler = new PListXMLHandler();
        parser.setHandler(handler);
        parser.parse(readFile("plist.xml"));
        PList pList = ((PListXMLHandler) parser.getHandler()).getPlist();
        Dict root = (Dict) pList.getRootElement();
        // This Array class is java.util.ArrayList<PListObject> underneath the
        // covers
        Array theList = root.getConfigurationArray("Objects");
        for (PListObject obj : theList) {
            switch (obj.getType()) {
                case DICT:
                    Dict dictionaryObj = (Dict) obj;
                    // dictionaryObj.getConfigurationObject(key);
                    // dictionaryObj.getConfigurationInteger(key);
                    // dictionaryObj.getConfiguration(key);
                    // dictionaryObj.getConfigurationArray(key)
                    break;

                case STRING:
                    com.longevitysoft.android.xml.plist.domain.String stringObj = (com.longevitysoft.android.xml.plist.domain.String) obj;
                    break;

                default:
                    break;
            }
        }
    }

    private static String readFile(String path) throws IOException {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded);
    }

}

当我尝试解析你的xml我有一些例外。那是因为PListXMLHandler正在检查localName而不是qualifiedName。这可以通过检查startElement()和endElement()方法中的localName来轻松修复。

When i tried parsing your xml i got some exception. That was because the PListXMLHandler was checking for localName and not qualifiedName. This could be easily fixed by checking for localName in startElement() and endElement() methods like.

if(isEmpty(localName)){
    localName = qName;
}

希望这有帮助。

这篇关于Android解析Plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆