Sunday, 14 October 2012

ANDROID - Sax Parsing (Using Url)

Demo:

1)SaxParsing Activity:


package com.venkool;

import java.io.IOException;
import java.io.InputStream;

public class SaxParsing extends Activity {
    /** Called when the activity is first created. */
     
      InputStream is;
      SAXParser saxParser;
      ListView lvList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        lvList=(ListView)findViewById(R.id.lvList);
       
       
       
        try {
            saxParser=SAXParserFactory.newInstance().newSAXParser();
                  is=new DefaultHttpClient().execute(
                              new HttpGet("http://www.xmlfiles.com/examples/cd_catalog.xml")).getEntity().getContent();
                  saxParser.parse(is, new MyHandler());
            } catch (IllegalStateException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } 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();
            } catch (ParserConfigurationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
            lvList.setAdapter(new MyArrayAdapter(new MyHandler().getData(),SaxParsing.this));
    }
}


2)MyArrayAdapter.java:

package com.venkool;

import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyArrayAdapter extends BaseAdapter {

      public Vector<Company> vecCompany;
      public Context context;
      public MyArrayAdapter(Vector<Company> vecCom,Context context) {
            // TODO Auto-generated constructor stub
            vecCompany=vecCom;
            this.context=context;
      }

      @Override
      public int getCount() {
            // TODO Auto-generated method stub
            if(vecCompany != null && vecCompany.size()>0)
            return vecCompany.size();
            else return 0;
      }

      @Override
      public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
      }

      @Override
      public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
      }

      @Override
      public View getView(int position, View v, ViewGroup parent) {
            // TODO Auto-generated method stub
           
            Company company=vecCompany.get(position);
           
            v=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.style, null);
           
            TextView tv1 = (TextView)v.findViewById(R.id.tvTitle);
            TextView tv2 = (TextView)v.findViewById(R.id.tvArtist);
            TextView tv3 = (TextView)v.findViewById(R.id.tvCountry);
            TextView tv4 = (TextView)v.findViewById(R.id.tvCompany);
            TextView tv5 = (TextView)v.findViewById(R.id.tvPrice);
            TextView tv6 = (TextView)v.findViewById(R.id.tvYear);
           
            tv1.setText(company.title);
            tv2.setText(company.artist);
            tv3.setText(company.country);
            tv4.setText(company.com);
            tv5.setText(company.price);
            tv6.setText(company.year);
           
            return v;
      }

     
}


3)MyHandler.java

package com.venkool;

import java.util.Vector;

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

public class MyHandler extends DefaultHandler {
     
      public static Vector<Company> vecCompany;
      Company company;
      StringBuffer currentString;
     
      @Override
      public void startElement(String uri, String localName, String qName,
                  Attributes attributes) throws SAXException {
            currentString = new StringBuffer();
            if(localName.equalsIgnoreCase("CATALOG"))
            {
                  if(vecCompany !=null && vecCompany.size()>0)
                        vecCompany.clear();
                  else
                        vecCompany=new Vector<Company>();
            }
           
            else if(localName.equalsIgnoreCase("CD"))
            {
                  company=new Company();
            }
           
      }
     
      @Override
      public void endElement(String uri, String localName, String qName)
                  throws SAXException {
            if(localName.equalsIgnoreCase("TITLE"))
            {
                  company.title=currentString.toString();
            }
           
            else if(localName.equalsIgnoreCase("ARTIST"))
            {
                  company.artist=currentString.toString();
            }
           
            else if(localName.equalsIgnoreCase("COUNTRY"))
            {
                  company.country=currentString.toString();
            }
           
            else if(localName.equalsIgnoreCase("COMPANY"))
            {
                  company.com=currentString.toString();
            }
           
            else if(localName.equalsIgnoreCase("PRICE"))
            {
                  company.price=currentString.toString();
            }
           
            else if(localName.equalsIgnoreCase("YEAR"))
            {
                  company.year=currentString.toString();
            }
           
            else if(localName.equalsIgnoreCase("CD"))
            {
                  vecCompany.add(company);
            }
      }
     
      @Override
      public void characters(char[] ch, int start, int length)
                  throws SAXException {
            currentString.append(ch, start, length);
      }
     
      public Vector<Company> getData()
      {
            return vecCompany;
      }

}


4)Company.java:

package com.venkool;

public class Company {
      String title;
      String artist;
      String country;
      String com;
      String price;
      String year;

}



5)main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/myBackgroundColor" >
    </ListView>

</LinearLayout>


6)style.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TITLE :" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="ARTIST :" />

        <TextView
            android:id="@+id/tvArtist"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="COUNTRY :" />

        <TextView
            android:id="@+id/tvCountry"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="COMPANY :" />

        <TextView
            android:id="@+id/tvCompany"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="PRICE :" />

        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="YEAR :" />

        <TextView
            android:id="@+id/tvYear"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
    </LinearLayout>

</LinearLayout>



7)Download this Project Click Here

4 comments: