Friday 12 October 2012

ANDROID - Sax Parsing (using raw data)

Demo:


1)SaxParsing1 Activity:

package com.venky.saxparsing1;

import java.io.IOException;
import java.util.Vector;

public class SAXParsing1Activity extends Activity {
      /** Called when the activity is first created. */

      ListView lvList;
      SAXParser saxParser;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            lvList = (ListView) findViewById(R.id.lvList);

            try {
                  saxParser = SAXParserFactory.newInstance().newSAXParser();
                  saxParser.parse(getResources().openRawResource(R.raw.sample),
                              new StudentHandler());
            } catch (ParserConfigurationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (SAXException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (NotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }

            lvList.setAdapter(new MyArrayAdapter(new StudentHandler().getData()));
      }

      public class MyArrayAdapter extends BaseAdapter {
            public Vector<Student> vecStudent;

            public MyArrayAdapter(Vector<Student> vecStu) {
                  vecStudent = vecStu;
            }

            @Override
            public int getCount() {
                  // TODO Auto-generated method stub
                  return vecStudent.size();
            }

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

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

            @Override
            public View getView(int position, View v, ViewGroup parent) {
                  Student student = vecStudent.get(position);
                  v = (LinearLayout) getLayoutInflater()
                              .inflate(R.layout.style, null);
                  TextView tv1 = (TextView) v.findViewById(R.id.tvSno);
                  TextView tv2 = (TextView) v.findViewById(R.id.tvSname);
                  TextView tv3 = (TextView) v.findViewById(R.id.tvScity);

                  tv1.setText(student.sno);
                  tv2.setText(student.sname);
                  tv3.setText(student.scity);
                  return v;
            }

      }
}


2)StudentHandler Activity:
 

package com.venky.saxparsing1;

import java.util.Vector;

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

public class StudentHandler extends DefaultHandler {
      public static Vector<Student> vecStudent;
      Student student;
      StringBuffer currentString;

      @Override
      public void startElement(String uri, String localName, String qName,
                  Attributes attributes) throws SAXException {
            if (localName.equalsIgnoreCase("College")) {
                  if (vecStudent != null && vecStudent.size() > 0) {
                        vecStudent.clear();
                  } else
                        vecStudent = new Vector<Student>();
            }

            else if (localName.equalsIgnoreCase("Student")) {
                  student = new Student();
            }
            currentString = new StringBuffer();
      }

      @Override
      public void endElement(String uri, String localName, String qName)
                  throws SAXException {
            if (localName.equalsIgnoreCase("sno")) {
                  student.sno = currentString.toString();
            }

            else if (localName.equalsIgnoreCase("sname")) {
                  student.sname = currentString.toString();
            }

            else if (localName.equalsIgnoreCase("scity")) {
                  student.scity = currentString.toString();
            }

            else if (localName.equalsIgnoreCase("Student")) {
                  vecStudent.add(student);
            }
      }

      @Override
      public void characters(char[] ch, int start, int length)
                  throws SAXException {
            currentString.append(ch, start, length);
      }

      public Vector<Student> getData() {
            return vecStudent;
      }

}


3)Student.java:
 

package com.venky.saxparsing1;

public class Student {
      String sno;
      String sname;
      String scity;
}


4)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:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sax Parsing Example"
        android:textSize="20dp"
        android:textStyle="bold" />

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Sno"
            android:textSize="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Sname"
            android:textSize="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Scity"
            android:textSize="15dp" />
    </LinearLayout>

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

</LinearLayout>


5)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:background="#5F04B4"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvSno"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/tvSname"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

    <TextView
        android:id="@+id/tvScity"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

</LinearLayout>


6)Download this Project Click Here
 

1 comment:

  1. thanks you sir giving this entire code.

    ReplyDelete