Friday 3 May 2013

How to parse plist in android and how to store the values hash map

Hi ,

   I am posting here one of my way to parse the .plist file in android and uses the data in more efficient way.For this parsing plist file initially you need to create plist file ..my plist file is like bellow.



<?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>student</key>
         <string>prudhvi</string>
         <key>marks</key>
         <string>100</string>
</dict>
<dict>
        <key>student</key>
        <string>srikanth</string>
        <key>marks</key>
        <string>99</string>
 </dict>
 <dict>
        <key>student</key>
        <string>srikanth</string>
        <key>marks</key>
        <string>99</string>
 </dict>
 <dict>
        <key>student</key>
        <string>rajesh</string>
        <key>marks</key>
        <string>98</string>
 </dict>
</array>
</plist>


And save this plist file with the extention of " .xml " .

Here we are going to main section to read values from your android application.

Step 1: Create the new project in eclipse and copy the your plist file into xml folder under the resources directory.



 

Step2 : In this step we are going to write the code to reading this plist file.I don't want write import statements .If any one need comment bellow.

Here i am going to explain how i manged the code line by line .Theme of output is parsing the plist and return the array list with hash map . hash map contain the key value pairs like below

         <key>student</key>
        <string>srikanth</string>

we store the student in  key place and "srikant" is in value place.If you want to retrieve the  value of student you can get easily.
       




public class ProductsPlistParsing {   
Context context;

   // constructor for  to get the context object from where you are using this plist parsing
    public ProductsPlistParsing(Context ctx) {

        context = ctx;
    }

    public List<HashMap<String, String>> getProductsPlistValues() {

       // specifying the  your plist file.And Xml ResourceParser is an event type parser for more details Read android source
        XmlResourceParser parser = context.getResources()
                .getXml(R.xml.products);


          // flag points to find key and value tags .
        boolean keytag = false;
        boolean valuetag = false;
        String keyStaring = null;
        String stringvalue = null;


        HashMap<String, String> hashmap = new HashMap<String, String>();
        List<HashMap<String, String>> listResult = new ArrayList<HashMap<String, String>>();
        int event;
        try {
            event = parser.getEventType();

             // repeting the loop at the end of the doccument

                 while (event != parser.END_DOCUMENT) {

               switch (event) {
                       //use switch case than the if ,else statements 
                case 0:
                        // start doccumnt nothing to do
                       // System.out.println("\n" + parser.START_DOCUMENT
                       // + "strat doccument");
                      // System.out.println(parser.getName());
                    break;
                case 1:
                    // end doccument
                    // System.out
                    // .println("\n" + parser.END_DOCUMENT + "end doccument");
                    // System.out.println(parser.getName());
                    break;
                case 2:

                    if (parser.getName().equals("key")) {
                        keytag = true;
                        valuetag = false;
                    }
                    if (parser.getName().equals("string")) {
                        valuetag = true;
                    }

                    break;
                case 3:
                    if (parser.getName().equals("dict")) {
                        System.out.println("end tag");
                        listResult.add(hashmap);
                        System.out.println(listResult.size() + "size");
                        hashmap = null;
                        hashmap = new HashMap<String, String>();
                    }
                    break;
                case 4:
                    if (keytag) {
                        if (valuetag == false) {
                            // hashmap.put("value", parser.getText());
                            // System.out.println(parser.getText());
                            // starttag = false;
                            keyStaring = parser.getText();
                        }
                    }
                    if (valuetag && keytag) {
                        stringvalue = parser.getText();

                        hashmap.put(keyStaring, stringvalue);
                        // System.out.println(keyStaring);
                        // System.out.println(stringvalue);
                        valuetag = false;
                        keytag = false;
                        // System.out.println("this is hash map"
                        // + hashmap.get(keyStaring));
                        // Toast.makeText(getApplication(), keyStaring,
                        // Toast.LENGTH_SHORT).show();

                    }
                    break;
                default:
                    break;
                }
                event = parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 //here you get the plistValues.
        return listResult;
    }
}



I managed the plist parsing in android like this.Hope it help full.If any errors you found please feel free to post comment.










2 comments:

  1. great work. helped me allot.

    ReplyDelete
  2. Hi, I cannot understand how to integrate to my app.
    Can you help me?

    ReplyDelete