Monday, February 13, 2012

Plist Android

Plist in Android:
             Plist is the property list files extension for MAC, iPhone OS programming frame works which has serialized objects that has been stored in it. Hence it is especially for iPhone application only, here we are going to use that files in our Android Application.The following is the sample application which used to read and display plist file values.

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.widget.TextView;

public class PlistSample extends Activity {
    /** Called when the activity is first created. */
    int i=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView plistValues = (TextView)findViewById(R.id.TextView01);
        String plistXmlContent;
        try
        {
             plistXmlContent = getValuesFromAnPlistXML(this);
            myXmlContent.setText(stringXmlContent);
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {   
            e.printStackTrace();
        }
    }
    private String  getValuesFromAnPlistXML (Activity activity) throws XmlPullParserException,IOException
    {
        StringBuffer stringBuffer = new StringBuffer();
        Resources res = activity.getResources();
        XmlResourceParser xpp = res.getXml(R.layout.plist);
        xpp.next();
        int eventType = xpp.getEventType();
        int flag1 = 0, flag2 = 0, flag3 = 0;
        while (eventType != XmlPullParser.END_DOCUMENT)
        {       
            if(eventType == XmlPullParser.START_TAG)
            {           
                String mainTag = xpp.getName().toString();
                if(mainTag.equals("dict"))
                {
                    if(flag1==0)
                    {
                    flag1 = 1;
                    i=i+1;
                    stringBuffer.append("\n Dict Tag Starts Number := "+i+"\n");
                    }
                    else
                    {
                        stringBuffer.append("Inner Dict Starts\n");
                        flag2 = 1;
                    }
                }           
                else if(mainTag.equals("key"))
                {
                    flag3 = 1;
                }           
                else
                {
                    flag3 = 2;
                }
            }         
            else if(eventType == XmlPullParser.END_TAG)
            {
                String g = xpp.getName().toString();           
                if(g.equals("dict"))
                {
                    if(flag1==1 && flag2!=1)
                    {               
                    stringBuffer.append("\n Main Dicts Ends\n");
                    flag1=0;
                    }
                    else
                    {
                        stringBuffer.append("\nInner Dicts Ends\n");
                        flag2=0;
                    }
                }
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                if(flag3 == 1)
                {
                    stringBuffer.append("\nkey:="+xpp.getText());
                    flag3 = 0;
                }
                else if(flag3 == 2)
                {
                    stringBuffer.append("\nValue:="+xpp.getText());
                    flag3 = 0;
                }
            }         
           eventType = xpp.next();
        }
        stringBuffer.append("\n--- End XML ---");
        stringBuffer.append("\n--- Total Number Of Dictionaries Are ---"+i);     
        return stringBuffer.toString();
    }
}

Create an XML file named plist.XML and copy the following plist code.


<?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">
<dict>
        <key>Label</key>
        <string>com.google.keystone.user.agent</string>
        <key>LimitLoadToSessionType</key>
        <string>Aqua</string>
        <key>ProgramArguments</key>
        <array>
          <string>/Users/al//Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Resources/GoogleSoftwareUpdateAgent.app/Contents/MacOS/GoogleSoftwareUpdateAgent</string>
          <string>-runMode</string>
          <string>ifneeded</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>3523</integer>
        <key>StandardErrorPath</key>
        <string>/dev/null</string>
        <key>StandardOutPath</key>
        <string>/dev/null</string>
</dict>
<dict>
        <key>label</key>
        <string>com.devdaily.pingwebsites</string>

        <key>ProgramArguments</key>
        <array>
                <string>/Users/al/bin/crontab-test.sh</string>
        </array>
        <key>OnDemand</key>
        <false/>
        <key>Nice</key>
        <integer>1</integer>
        <key>StartInterval</key>
        <integer>60</integer>
        <key>StandardErrorPath</key>
        <string>/tmp/AlTest1.err</string>
        <key>StandardOutPath</key>
        <string>/tmp/AlTest1.out</string>
</dict>
</plist> <?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">
<dict>

1 comment:

Anonymous said...

Please upload the sample code on any place like github or your own bog