Thursday, June 23, 2011

XML parsing using SAX with Listview

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/name" />
    <ListView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/list" />
</LinearLayout>

listitem.xml

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



XMLParsindSAXParser.java

package com.parsingbySAX;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class XMLParsindSAXParser extends Activity {

    SiteList siteList = null;
    static ArrayList<String> h1visa = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView name;
        ListView k;

        name = (TextView) findViewById(R.id.name);
        k = (ListView) findViewById(R.id.list);

        try {

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            URL sourceUrl = new URL(
                    "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            // TODO: handle exception

            System.out.println("XML Parsing Exception : " + e);
        }
        siteList = MyXMLHandler.siteList;
        int h = siteList.getName().size();
        for (int i = 0; i < siteList.getName().size(); i++) {

            h1visa.add(siteList.getName().get(i));
            Log.v("log_tag", "new data  " + h1visa);

        }
        k.setAdapter(new EfficientAdapter(this, h1visa));

        k.setTextFilterEnabled(true);

        // setContentView(layout);
    }

    public static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private List<String> data;

        public EfficientAdapter(Context context, List<String> data) {
            mInflater = LayoutInflater.from(context);
            data = this.data;
        }

        public int getCount() {
            return h1visa.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listitem, null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(h1visa.get(position));
            return convertView;
        }

        static class ViewHolder {
            TextView text;

        }
    }

}



SiteList.java

package com.parsingbySAX;

import java.util.ArrayList;

public class SiteList {
   
    private ArrayList<String> name=new ArrayList<String>();
    private ArrayList<String> website=new ArrayList<String>();
    private ArrayList<String> category=new ArrayList<String>();
   
   
    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);
    }
    public ArrayList<String> getCategory(){
        return category;
    }
    public void setCategory(String category){
        this.category.add(category);
    }

}

MyXMLHandler.java

package com.parsingbySAX;

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

public class MyXMLHandler extends DefaultHandler{
   
    boolean currentElement = false;
    String currentValue = null;

    public static SiteList siteList=null;
   
    public static SiteList getSiteList(){
        return siteList;
    }
   
    public static void setSiteList(SiteList siteList){
       
        MyXMLHandler.siteList=siteList;
    }
   
    public void startElement(String uri,String localName,String qName,Attributes attrubutes) throws SAXException{
        currentElement=true;
       
        if(localName.equals("maintag")){
            siteList=new SiteList();
        }
        else if(localName.equals("website")){
            String attr=attrubutes.getValue("category");
            siteList.setCategory(attr);
        }
    }
   
    public void endElement(String uri,String localName,String qName) throws SAXException{
       
        currentElement=false;
       
        if(localName.equalsIgnoreCase("name")){
            siteList.setName(currentValue);
           
        }
        else if(localName.equalsIgnoreCase("website")){
            siteList.setWebsite(currentValue);
        }
       
    }
    public void characters(char[] ch,int start,int length) throws SAXException{
       
        if(currentElement){
            currentValue=new String(ch, start, length);
            currentElement=false;
        }
    }
}

6 comments:

  1. xml is a very interesting language to be used and contain the data object model or abbreviated DOM.tutorial very good and hopefully can help me in building a web application thanks

    ReplyDelete
  2. Thanx for post...
    but how to click on link we get from parsing

    ReplyDelete
  3. how to write click event and send an object to another activity?

    ReplyDelete