plist file from assets

71 views
Skip to first unread message

vampke

unread,
Mar 4, 2014, 3:21:24 PM3/4/14
to plist-...@googlegroups.com
Hi,

I want to parse a plist file that is in my android assets folder.
How do you do this? You can't just use the File constructir for the items in assets but you need InputStream.
Help would be much appreciated!

Kind regards,

v

Daniel Dreibrodt

unread,
Mar 4, 2014, 3:24:14 PM3/4/14
to plist-discuss
Hi,

just get an InputStream for your resource. For that it should be located in the res/raw/ folder.

Then either use

InputStream is = getResources().openRawResource(R.raw.YourPlistFile);

or

InputStream is = context.getAssets().open("YourPlistFile?.plist");

and then

NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(is);

(See http://stackoverflow.com/questions/2856407/android-how-to-get-access-to-raw-resources-that-i-put-in-res-folder)

Regards,

Daniel

An 04.03.2014 21:21:25, vampke <rescomw...@gmail.com>geschrieben:

--
You received this message because you are subscribed to the Google Groups "plist-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plist-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

vampke

unread,
Mar 4, 2014, 4:05:28 PM3/4/14
to plist-...@googlegroups.com


On Tuesday, March 4, 2014 9:24:14 PM UTC+1, Daniel D. wrote:
Hi,

just get an InputStream for your resource. For that it should be located in the res/raw/ folder.

Then either use

InputStream is = getResources().openRawResource(R.raw.YourPlistFile);

or

InputStream is = context.getAssets().open("YourPlistFile?.plist");

and then

NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(is);


Thank you very much for your swift reply. I was not expecting an answer this soon :)
I have tried both ways but I receive an error: Type mismatch: cannot convert from InputStream to Base64.InputStream
If I cast the result to inputstream, like "InputStream inputStream = (InputStream) getResources().openRawResource(R.raw.plistfile);" a java.lang.castException is thrown
Any ideas?

vampke

unread,
Mar 4, 2014, 4:20:30 PM3/4/14
to plist-...@googlegroups.com
Update: it seems there is an inputstream in your class as well!
I had to use the java.io.InputStream, this resolved this error (allthough I am still experiencing others, will try to figure them out first).
Thanks anyway for your help and excellent work on writing this class!

Daniel Dreibrodt

unread,
Mar 4, 2014, 4:29:48 PM3/4/14
to plist-...@googlegroups.com

Yeah, i was wondering about that strange error, please just use the java.io.InputStream class. Parsing should work fine now.

Regards,
Daniek

--

vampke

unread,
Mar 4, 2014, 4:50:51 PM3/4/14
to plist-...@googlegroups.com
hmmmm, unfortunately still no luck: now I keep getting this error: "java.lang.ClassCastException: com.dd.plist.NSArray" for this code:
            NSDictionary rootDict = null;
            try {
                rootDict = (NSDictionary)PropertyListParser.parse(inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }

The reason seems to be in the fact that my plist file (which works fine in ios) is an array of dictionnaries:

<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
...key-string pairs
</dict>
<dict>
...
</array>
</plist>
If I make a new plist without the array and only one <dict> it seems to be working.
Is this a problem with the parser class or of the plist?
(or, also very likely, of my poor android coding skills :/)


Daniel Dreibrodt

unread,
Mar 4, 2014, 5:53:44 PM3/4/14
to plist-...@googlegroups.com
The error indicates that a NSArray is returned by the parse code. So you cannot cast it to a NSDictionary. Use NSArray rootArray = (NSArray)PropertyListParser.parse(inputStream);

The parse method will return either an array or a dictionary, depending on the root node of the property list. So after you changed your plist file to contain only one dictionary as root the previous parsing code worked fine.

vampke

unread,
Mar 5, 2014, 10:49:54 AM3/5/14
to plist-...@googlegroups.com
Thank you very much for your kind and selfless help. I got everything to work fine now using

            InputStream inputStream = getResources().openRawResource(resourceId);
            NSArray rootArray = null;
            try {
                rootArray = (NSArray) PropertyListParser.parse(inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
            NSObject[] my_object = rootArray.getArray();
            for(NSObject param:my_object) {
                NSDictionary title = (NSDictionary) param;
etc...

Keep up the good work!
Reply all
Reply to author
Forward
0 new messages