Friday, 6 September 2013

Xml Parsing

MainActivity.java

package com.eds.demo;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    /** Create Object For SiteList Class */
    SitesList sitesList = null;
    int i=0,j=1,k=2;
    ImageView im1,im2,im3;
     ProgressDialog progress;
     XMLReader xr;
    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
         im1=(ImageView)findViewById(R.id.image1);
         im2=(ImageView)findViewById(R.id.image2);
         im3=(ImageView)findViewById(R.id.image3);
         try {
            
             /** Handling XML */
             SAXParserFactory spf = SAXParserFactory.newInstance();
             SAXParser sp = spf.newSAXParser();
               xr = sp.getXMLReader();
             MyXMLHandler myXMLHandler = new MyXMLHandler();
             xr.setContentHandler(myXMLHandler);
        
             new Myasynch().execute();
            
            
         } catch (Exception e) {
             System.out.println("XML Pasing Excpetion = " + e);
         }
       
      
    }
    public void click(View V)
    {  
        if(i==sitesList.getWebsite().size())
        {
            i=0;
        }
       
        try {
            ++i;
            if(i==sitesList.getWebsite().size())
            {
                i=0;
            }
            im1.setImageDrawable(Drawable.createFromStream((InputStream)new URL(sitesList.getWebsite().get(i)).getContent(), "src"));
            ++j;
            if(j==sitesList.getWebsite().size())
            {
                j=0;
            }
           
            im2.setImageDrawable(Drawable.createFromStream((InputStream)new URL(sitesList.getWebsite().get(j)).getContent(), "src"));
            ++k;
            if(k==sitesList.getWebsite().size())
            {
                k=0;
            }
            im3.setImageDrawable(Drawable.createFromStream((InputStream)new URL(sitesList.getWebsite().get(k)).getContent(), "src"));
           
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }

    class Myasynch extends AsyncTask<Void, Void, Void> {

        ProgressDialog progress;
        InputStream ins;
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            progress.cancel();
          //  Toast.makeText(getApplicationContext(),imageName,10000).show();
            sitesList = MyXMLHandler.sitesList;
            try {
                im1.setImageDrawable(Drawable.createFromStream((InputStream)new URL(sitesList.getWebsite().get(0)).getContent(), "src"));
                im2.setImageDrawable(Drawable.createFromStream((InputStream)new URL(sitesList.getWebsite().get(1)).getContent(), "src"));
                im3.setImageDrawable(Drawable.createFromStream((InputStream)new URL(sitesList.getWebsite().get(2)).getContent(), "src"));
               
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          
           Toast.makeText(getApplicationContext(), sitesList.getWebsite().get(1),20000).show();
                    super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            progress = ProgressDialog.show(MainActivity.this,
                    "", "Searching....");
            progress.setCancelable(true);
            super.onPreExecute();
        }
        
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            HttpClient http_client = new DefaultHttpClient();

            HttpGet get = new HttpGet("http://clientextranet.in/android/android_test.php");

            try {
                HttpResponse response = http_client.execute(get);

                ins = response.getEntity().getContent();
               
                xr.parse(new InputSource(ins));

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

       

    }

}
MyXMLHandler.jav:-

package com.eds.demo;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static SitesList sitesList = null;
    public static SitesList getSitesList()
    {
        return sitesList;
    }
   
    public static void setSitesList(SitesList sitesList)
    {
        MyXMLHandler.sitesList = sitesList;
    }
    /** Called when tag starts ( ex:- <name>AndroidPeople</name>
    * -- <name> )*/
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException
    {
        currentElement = true;
        if (localName.equals("maintag"))
        {
            /** Start */
            sitesList = new SitesList();
        }
     
    }
    /** Called when tag closing ( ex:- <name>AndroidPeople</name>
    * -- </name> )*/
    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException
    {
        currentElement = false;
        /** set value */
        if (localName.equalsIgnoreCase("name")){
            sitesList.setName(currentValue);
        Log.d("SAGAR", "NAME "+currentValue);}
        else if (localName.equalsIgnoreCase("url"))
        {
            sitesList.setWebsite(currentValue);
        Log.d("SAGAR", "url "+currentValue);}
    }
    /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
    * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException
    {
        if (currentElement)
        {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }
    }
}

SitesList.java:-

package com.eds.demo;

import java.util.ArrayList;

/** Contains getter and setter method for varialbles  */
public class SitesList {

      /** Variables */
    private ArrayList<String> name = new ArrayList<String>();
    private ArrayList<String> website = new ArrayList<String>();
  
    /** In Setter method default it will return arraylist
    * change that to add */
    public ArrayList<String> getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name.add(name);
    }
    public ArrayList<String> getWebsite()
    {
        return website;
    }
    public void setWebsite(String website)
    {
        this.website.add(website);
    }
  

}

No comments:

Post a Comment