简体   繁体   English

Android解析Plist文件

[英]Android parse Plist file

Hello Im trying to parse an plist file that contains array of dict's. 您好我试图解析包含dict数组的plist文件。 Im trying to do this using xmlwise . 我试图用xmlwise来做这件事 The content of the plistfile is here plistfile的内容在这里

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? 到目前为止,我只在我的活动中有这个和我获取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();
    }
}

Quick question. 快问。 What should be the content of the ArrayList? 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图中提到了一个Object列表,那么为什么你只能从地图中获取值

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

Apart from that checking your plist the structure is like a Dict root element and a list of Dicts in it. 除了检查你的plist之外,结构就像一个Dict根元素和一个Dicts列表。 I assume you need to get this as a list. 我假设您需要将此作为列表。 If so consider using Android PList parser by longevitysoft. 如果是这样,请考虑使用longevitysoft的Android PList解析器 This is simple and opensource. 这很简单,开源。 Its basically a SAXParser parsing Apple PList . 它基本上是一个SAXParser解析Apple PList

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 在你的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);
    }

}

When i tried parsing your xml i got some exception. 当我尝试解析你的xml我有一些例外。 That was because the PListXMLHandler was checking for localName and not qualifiedName. 那是因为PListXMLHandler正在检查localName而不是qualifiedName。 This could be easily fixed by checking for localName in startElement() and endElement() methods like. 通过检查startElement()和endElement()方法中的localName可以很容易地解决这个问题。

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

Hope this helps. 希望这可以帮助。

You can also try Google dd-plist.jar libraries or SAXON parse for parsing plist. 您还可以尝试使用Google dd-plist.jar库或SAXON解析来解析plist。

Go through this conversion : 完成此转换:

https://code.google.com/p/plist/wiki/Examples http://developer.android.com/reference/javax/xml/parsers/SAXParser.html https://code.google.com/p/plist/wiki/Examples http://developer.android.com/reference/javax/xml/parsers/SAXParser.html

You can use dd-plist jar for doing this, Download dd-plist.jar from Google its nice and fast. 您可以使用dd-plist jar来执行此操作,从Google下载dd-plist.jar非常快速。

I am putting an example working for me from Google code colud. 我正在通过Google代码colud为我工作。 Link is here. 链接在这里。 http://plist.googlecode.com/svn-history/r61/trunk/src/com/dd/plist/XMLPropertyListParser.java http://plist.googlecode.com/svn-history/r61/trunk/src/com/dd/plist/XMLPropertyListParser.java

package com.dd.plist.test;

import com.dd.plist.*;
import java.io.File;
import java.util.Arrays;
import java.util.Date;
import junit.framework.TestCase;

public class ParseTest extends TestCase {

    /**
     * Test the xml reader/writer
     */
    public static void testXml() throws Exception {
        System.out.println(new File("test-files/"));

        // parse an example plist file
        NSObject x = PropertyListParser.parse(new File("test-files/test1.plist"));

        // check the data in it
        NSDictionary d = (NSDictionary)x;
        assertTrue(d.count() == 5);
        assertTrue(((NSString)d.objectForKey("keyA")).toString().equals("valueA"));
        assertTrue(((NSString)d.objectForKey("key&B")).toString().equals("value&B"));
        assertTrue(((NSDate)d.objectForKey("date")).getDate().equals(new Date(1322472090000L)));
        assertTrue(Arrays.equals(((NSData)d.objectForKey("data")).bytes(),
                                 new byte[]{0x00,0x00,0x00,0x04,0x10,0x41,0x08,0x20,(byte)0x82}));
        NSArray a = (NSArray)d.objectForKey("array");
        assertTrue(a.count() == 4);
        assertTrue(a.objectAtIndex(0).equals(new NSNumber(true)));
        assertTrue(a.objectAtIndex(1).equals(new NSNumber(false)));
        assertTrue(a.objectAtIndex(2).equals(new NSNumber(87)));
        assertTrue(a.objectAtIndex(3).equals(new NSNumber(3.14159)));

        // read/write it, make sure we get the same thing
        PropertyListParser.saveAsXML(x, new File("test-files/out-testXml.plist"));
        NSObject y = PropertyListParser.parse(new File("test-files/out-testXml.plist"));
        assertTrue(x.equals(y));
    }

    /**
     *  Test the binary reader/writer.
     */
    public static void testBinary() throws Exception {
        NSObject x = PropertyListParser.parse(new File("test-files/test1.plist"));

        // save and load as binary
        PropertyListParser.saveAsBinary(x, new File("test-files/out-testBinary.plist"));
        NSObject y = PropertyListParser.parse(new File("test-files/out-testBinary.plist"));
        assertTrue(x.equals(y));
    }

    /**
     *  NSSet only occurs in binary property lists, so we have to test it separately.
     */
    public static void testSet() throws Exception {
        NSSet s = new NSSet();
        s.addObject(new NSNumber(1));
        s.addObject(new NSNumber(2));
        s.addObject(new NSNumber(3));

        PropertyListParser.saveAsBinary(s, new File("test-files/out-testSet.plist"));
        NSObject t = PropertyListParser.parse(new File("test-files/out-testSet.plist"));
        assertTrue(s.equals(t));
    }

    public static void testASCII() throws Exception {
        NSObject x = PropertyListParser.parse(new File("test-files/test1-ascii.plist"));
        NSDictionary d = (NSDictionary)x;        
        assertTrue(d.count() == 5);        
        assertTrue(((NSString)d.objectForKey("keyA")).toString().equals("valueA"));
        assertTrue(((NSString)d.objectForKey("key&B")).toString().equals("value&B"));
        assertTrue(((NSDate)d.objectForKey("date")).getDate().equals(new Date(1322472090000L)));
        assertTrue(Arrays.equals(((NSData)d.objectForKey("data")).bytes(),
                                 new byte[]{0x00,0x00,0x00,0x04,0x10,0x41,0x08,0x20,(byte)0x82}));
        NSArray a = (NSArray)d.objectForKey("array");
        assertTrue(a.count() == 4);
        assertTrue(a.objectAtIndex(0).equals(new NSNumber(true)));
        assertTrue(a.objectAtIndex(1).equals(new NSNumber(false)));
        assertTrue(a.objectAtIndex(2).equals(new NSNumber(87)));
        assertTrue(a.objectAtIndex(3).equals(new NSNumber(3.14159)));
        NSObject y = PropertyListParser.parse(new File("test-files/test1-ascii-gnustep.plist"));
        assertTrue(x.equals(y));
    }

    public static void testASCIIWriting() throws Exception {
        File in = new File("test-files/test1.plist");
        File out = new File("test-files/out-test1-ascii.plist");
        NSDictionary x = (NSDictionary)PropertyListParser.parse(in);
        PropertyListParser.saveAsASCII(x, out);
        NSDictionary y = (NSDictionary)PropertyListParser.parse(out);
        assertTrue(x.equals(y));
    }

    public static void testGnuStepASCIIWriting() throws Exception {
        File in = new File("test-files/test1.plist");
        File out = new File("test-files/out-test1-ascii-gnustep.plist");
        NSDictionary x = (NSDictionary)PropertyListParser.parse(in);
        PropertyListParser.saveAsGnuStepASCII(x, out);
        NSObject y = PropertyListParser.parse(out);
        assertTrue(x.equals(y));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM