plist Parsing on Android

Tutorials with advanced 'difficulty' and more Lines of Code.

plist Parsing on Android

Postby stelioschar » Wed Sep 29, 2010 1:16 pm

Hello to all,

I have a project and the data that i have to parse are on plists and looking around for some tutorials, nobody use plists.

Is it possible to parse plists on Android, or i'm wasting my time??
stelioschar
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Aug 19, 2010 9:18 am

Top

Re: plist Parsing on Android

Postby stelioschar » Thu Sep 30, 2010 9:47 am

Anyone who can help me?
stelioschar
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Aug 19, 2010 9:18 am

Re: plist Parsing on Android

Postby stelioschar » Fri Oct 01, 2010 9:36 am

Below there is a typical plist template
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  <plist version="1.0">
  2.     <dict>
  3.         <key>key</key>
  4.         <string><![CDATA[data]]></string>
  5.         <key>key</key>
  6.        
  7.        
  8.             <string>string</string>
  9.  
  10.        
  11.         <key>key</key>
  12.         <string><![CDATA[string]]></string>
  13.         <key>key</key>
  14.         <string>string</string>
  15.         <key>key</key>
  16.         <array>
  17.            
  18.                 <dict>
  19.                     <key>key</key>
  20.                     <string><![CDATA[string]]></string>
  21.                     <key>key</key>
  22.                     <integer>integer</integer>
  23.                     <key>key</key>
  24.                     <string><![CDATA[string]]></string>
  25.                     <key>key</key>
  26.                     <string>string</string>
  27.                 </dict>
  28.            
  29.         </array>
  30.         <array>
  31.            
  32.                 <dict>
  33.                     <key>key</key>
  34.                     <string><![CDATA[string]]></string>
  35.                     <key>key</key>
  36.                     <integer>integer</integer>
  37.                     <key>key</key>
  38.                     <string><![CDATA[string]]></string>
  39.                     <key>key</key>
  40.                     <string>string</string>
  41.                 </dict>
  42.            
  43.         </array>
  44.     </dict>
  45.  </plist>      
  46.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Someone who can give me an advice??

Thanks in advance..
stelioschar
Junior Developer
Junior Developer
 
Posts: 18
Joined: Thu Aug 19, 2010 9:18 am

Re: plist Parsing on Android

Postby akoscz » Fri Nov 12, 2010 10:53 am

Here is a class I wrote to parse an xml plist file. It uses the XmlPullParser to do the parsing. I have only implemented what I needed for my project. But this should get you started for extending the class if you need more than what this class provides.

Code: Select all
package com.paperlit.reader.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

import org.xmlpull.v1.XmlPullParser;

//import android.util.Log;
import android.util.Xml;

/**
* This class will parse a plist xml file and store the contents in a
* hierarchical HashMap of <String, Object> tuples, where the Object value
* could be another HashMap, an ArrayList, String or Integer value.
*
* Use the getConfiguration methods to retrieve the values from the parsed plist file.
*
* The key names for nested dictionary references must be of the form :
*       Dict1KeyName.Dict2KeyName.ElementKeyName
*
* @author akoscz
*
*/
public class XMLPropertyListConfiguration {

   //private static final String TAG = "plist";
   
   /**
    * The nested (hierarchical) HashMap which holds our key-value pairs of our plist file.
    */
   protected HashMap<String, Object> mPlistHashMap;

   /**
    * Constructor.
    * Parse a plist file from the given InputStream.
    *
    * @param inputStream The InputStream which has the bytes of the plist file we need to parse.
    */
   public XMLPropertyListConfiguration(InputStream inputStream) {
      mPlistHashMap = new HashMap<String, Object>();
      parse(inputStream);
   }

   /**
    * Get an String configuration value for the given key.
    *
    * @param keyName The name of the key to look up in the configuration dictionary.
    * @return The String value of the specified key.
    */
   public String getConfiguration(String keyName) {
      return (String)getConfigurationObject(keyName);
   }

   /**
    * Get a String configuration value for the given key.
    * If there is no value for the given key, then return the default value.
    * @param keyName The name of the key to look up in the configuration dictionary.
    * @param defaultValue The default value to return if they key has no associated value.
    * @return The String value of the specified key, or defaultValue if the value for keyName is null.
    */
   public String getConfigurationWithDefault(String keyName, String defaultValue) {
      String value = getConfiguration(keyName);
      if(value == null) {
         return defaultValue;
      }
      
      return value;
   }

   /**
    * Get an Integer configuration value for the given key.
    *
    * @param keyName The name of the key to look up in the configuration dictionary.
    * @return The Integer value of the specified key.
    */
   public Integer getConfigurationInteger(String keyName) {
      return (Integer)getConfigurationObject(keyName);
   }

   /**
    * Get an Integer configuration value for the given key.
    * If there is no value for the given key, then return the default value.
    * @param keyName The name of the key to look up in the configuration dictionary.
    * @param defaultValue The default value to return if they key has no associated value.
    * @return The Integer value of the specified key, or defaultValue if the value for keyName is null.
    */
   public Integer getConfigurationIntegerWithDefault(String keyName, Integer defaultValue) {
      Integer value = getConfigurationInteger(keyName);
      if(value == null) {
         return defaultValue;
      }
      
      return value;
   }

   /**
    * Utility method which uses a XmlPullParser to iterate through the XML elements
    * and build up a hierarchical HashMap representing the key-value pairs of the
    * plist configuration file.
    *
    * @param inputStream The InputStream which contains the plist XML file.
    */
   public void parse(InputStream inputStream) {
      XmlPullParser parser = Xml.newPullParser();
      
      try {
         parser.setInput(inputStream, null);
         
         int eventType = parser.getEventType();
         
         boolean done = false;
         boolean parsingArray = false;
         
         String name = null;
         String key = null;
         
         Stack<HashMap<String, Object>> stack = new Stack<HashMap<String, Object>>();
         HashMap<String, Object> dict = null;
         ArrayList<Object> array = null;
         
         while (eventType != XmlPullParser.END_DOCUMENT && !done) {
            switch(eventType) {
               case XmlPullParser.START_DOCUMENT:
                  //Log.d(TAG, "START_DOCUMENT");
                  break;
               case XmlPullParser.START_TAG:
                  name = parser.getName();
                  
                  if(name.equalsIgnoreCase("dict")) {
                     // root dict element
                     if(key == null) {
                        mPlistHashMap.clear();
                        dict = mPlistHashMap;
                     }
                     else if(parsingArray) {
                        //Log.d(TAG, "START_TAG dict : inside array");
                        HashMap<String, Object> childDict = new HashMap<String, Object>();
                        array.add(childDict);
                        stack.push(dict);
                        dict = childDict;
                     }
                     else {
                        //Log.d(TAG, "START_TAG dict : " + key);
                        HashMap<String, Object> childDict = new HashMap<String, Object>();
                        dict.put(key, childDict);
                        stack.push(dict);
                        dict = childDict;
                     }
                  }
                  else if(name.equalsIgnoreCase("key")) {
                     key = parser.nextText();
                  }
                  else if(name.equalsIgnoreCase("integer")) {
                     dict.put(key, new Integer(parser.nextText()));
                  }
                  else if(name.equalsIgnoreCase("string")) {
                     if(parsingArray) {
                        array.add(parser.nextText());
                     } else {
                        dict.put(key, parser.nextText());
                     }
                  }
                  else if(name.equalsIgnoreCase("array")) {
                     parsingArray = true;
                     array = new ArrayList<Object>();
                     dict.put(key, array);
                  }
   
                  break;
               case XmlPullParser.END_TAG:
                  name = parser.getName();
   
                  if(name.equalsIgnoreCase("dict")) {
                     //Log.d(TAG, "END_TAG dict");
                     if(!stack.empty()) {
                        dict = stack.pop();
                     }
                  }
                  else if(name.equalsIgnoreCase("array")) {
                     parsingArray = false;
                  }
                  else if(name.equalsIgnoreCase("plist")) {
                     done = true;
                  }
   
                  break;
               case XmlPullParser.END_DOCUMENT:
                  //Log.d(TAG, "END_DOCUMENT");
                  break;
   
            }
            eventType = parser.next();
         }
      }catch (Exception ex){
         ex.printStackTrace();
      }      
   }


   /**
    * Utility method which tokenizes the given keyName using
    * the "." delimiter and then looks up each token in the
    * configuration dictionary.  If the token key points to a
    * dictionary then it proceeds to the next token key and
    * looks up value of the token key in the dictionary it found
    * from the previous token key.
    *
    * @param keyName The fully qualified key name.
    * @return The Object value associated with the given key, or null if the key does not exist.
    */
   @SuppressWarnings("unchecked")
   private Object getConfigurationObject(String keyName) {
      String[] tokens = keyName.split("\\.");
   
      if(tokens.length > 1) {
         HashMap<String,Object> dict = mPlistHashMap;
         Object obj;
         for(int i = 0; i < tokens.length; i++) {
            obj = dict.get(tokens[i]);
            if(obj instanceof HashMap<?, ?>) {
               dict = (HashMap<String, Object>) obj;
               continue;
            }
            return obj;
         }
      }
      
      return mPlistHashMap.get(keyName);
   }
}
akoscz
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 12, 2010 10:44 am

Re: plist Parsing on Android

Postby freebeachler » Wed Dec 29, 2010 7:32 am

You can find an alternate version of a PList parser for Android at https://github.com/tenaciousRas/android-plist-parser.
freebeachler
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 29, 2010 7:30 am

Re: plist Parsing on Android

Postby blundell » Tue Jan 04, 2011 3:58 pm

Parsing a Plist is just like parsing any other XML file.

Use this tut:

parsing_xml_from_the_net_-_using_the_saxparser-t353.html
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: plist Parsing on Android

Postby kpbird » Tue Aug 02, 2011 7:40 am

Hello guys,

one more example of plist parser..

http://kpbird.blogspot.com/2011/08/andr ... arser.html
kpbird
Freshman
Freshman
 
Posts: 9
Joined: Fri Oct 03, 2008 1:40 pm

Re: plist Parsing on Android

Postby Nickh186 » Fri Aug 19, 2011 3:05 am

I had to parse a plist for an app I was working on when I was sort of new to Android, and it sounds a lot more difficult than it is. This thread is spot on with how to approach it, just treat it like any XML file. Blundell's link to a tutorial on these forums is great place to start.

The only specific thing I did (as a matter of preference) was to use the SAXParser. I just found it more intuitive. Again, just my two cents.
Nickh186
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Aug 19, 2011 2:58 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests