java – Android解析Plist文件

前端之家收集整理的这篇文章主要介绍了java – Android解析Plist文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
您好我试图解析包含dict数组的plist文件.我试图使用 xmlwise执行此操作.pist文件内容here

到目前为止,我只在我的活动中有这个和我获取plistfile的内容,但如何将内容解析为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中的Object列表< String,Object>属性映射然后为什么你不能从地图中获取
Map<String,Object> properties = new HashMap<String,Object>();
List<Object> plist = new ArrayList<Object>(properties.values());

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

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

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可以很容易地解决这个问题.

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

希望这可以帮助.

猜你在找的Android相关文章