Tuesday, 16 October 2012

ANDROID - Sax Parsing 1
























Using URL = http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml

Permission in manifest:
<uses-permission android:name="android.permission.INTERNET"/>

Java Classes:
1)SaxParser_1Activity.java:

package com.venkool;

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

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

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class SaxParser_1Activity 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 {
is = new DefaultHttpClient()
.execute(
new HttpGet(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml"))
.getEntity().getContent();
saxParser = SAXParserFactory.newInstance().newSAXParser();
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 (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

lvList.setAdapter(new MyArrayAdapter(new MyHandler().getData(),
SaxParser_1Activity.this));

}
}


2)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;

import android.content.Context;

public class MyHandler extends DefaultHandler {

public static Vector<Item> vecItem;
Item item;
StringBuffer currentString;

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

else if(localName.equalsIgnoreCase("item"))
{
item=new Item();
}
currentString=new StringBuffer();
}

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

else if(localName.equalsIgnoreCase("website"))
{
item.website=currentString.toString();
}

else if(localName.equalsIgnoreCase("item"))
{
vecItem.add(item);
}
}

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

public Vector<Item> getData()
{
return vecItem;
}

}

3)Item.java

package com.venkool;

public class Item {

String name;
String website;
}


4)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<Item> vecItem;
public Context context;

public MyArrayAdapter(Vector<Item> data,
SaxParser_1Activity saxParser_1Activity) {
// TODO Auto-generated constructor stub
vecItem = data;
this.context = saxParser_1Activity;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
if(vecItem !=null && vecItem.size()>0)
return vecItem.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
Item item = vecItem.get(position);
v = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.style,
null);

TextView tv1 = (TextView) v.findViewById(R.id.tvName);
TextView tv2 = (TextView) v.findViewById(R.id.tvWeb);

tv1.setText(item.name);
tv2.setText(item.website);

return v;
}

}

Xml Files:

1)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" >

    <TextView
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sax Parsing Example" />
    <ListView 
        android:background="@color/myColor"
        android:id="@+id/lvList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        
    </ListView>

</LinearLayout>


2)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" >

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

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

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

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

</LinearLayout>

No comments:

Post a Comment