Custom Search

Android:Xml parsing Common class

Xml parsing in android


Here is the simplest way to parse xml and displaying into list view accordingly.You just have to care about how many tag you want  from your xml.accordingly create a Xml and base adapter.Now work is finish.

If you want to use this commong xml file in  more than one listview with different design then only create switch case.


package com.demo.ahmad;

import java.util.ArrayList;

import com.demo.ahmad.ParsingXml.CommonClassFunction;

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

public class XmlAhmadCommonActivity extends Activity {
    /** Called when the activity is first created. */
private CommonClassFunction ccF;
ParsingXml sm=new ParsingXml();private ArrayList<String> tagName=new ArrayList<String>();
private ListView list;private ProgressDialog pd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list=(ListView) findViewById(R.id.list);
        tagName.add("vSmallImageDetail");tagName.add("vDescriptionDetail");tagName.add("vDurationDetail");tagName.add("dAddedDateDetail");
        pd=ProgressDialog.show(this,"Please wait","Loading...");
        pd.setCancelable(false);
        String url="www.demo.com";
        ccF=sm.new CommonClassFunction(this,tagName,list,pd,url);
        ccF.start();
    }
}



Common class that will perform everything in background thread and will display data in ListView

package com.demo.ahmad;

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

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

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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

public class ParsingXml extends DefaultHandler{
private ArrayList<String> [] store;private int length;
private ListView listview;private ProgressDialog pd;
// private String success="";
private boolean Error=false,flag=false;private ArrayList<String> tagName=new ArrayList<String>();private Activity act;
@SuppressWarnings("unchecked")
public ParsingXml(ArrayList<String> tag,int len){
length=len;store=new ArrayList[length];
tagName=tag;
for(int i=0;i<length;i++){
store[i]=new ArrayList<String>();
}
}
public ParsingXml(){
}
@Override
public void characters(char[] ch, int start, int lengthh)
throws SAXException {
super.characters(ch, start, lengthh);
String str=new String(ch,start,lengthh);
str=str.trim();
if(flag){
for(int i=0;i<length;i++){
if(tagName.get(i).equalsIgnoreCase(local)){
store[i].add(str);
}
}
}
}
    String local="";
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
for(int i=0;i<length;i++){
if(tagName.get(i).equalsIgnoreCase(localName)){
flag=false;
}
}
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
for(int i=0;i<length;i++){
if(tagName.get(i).equalsIgnoreCase(localName)){
flag=true;local=localName;
}
}
}

private String str;
public class CommonClassFunction extends Thread{

public CommonClassFunction(Activity actt,ArrayList<String> tag,ListView list,ProgressDialog pdD,String url){
act=actt;tagName=tag;listview=list;pd=pdD;str=url;
}
@Override
public void run() {
try{
length=tagName.size();
act.runOnUiThread(all);
}
catch(Exception e){
e.getMessage();
}
}
}
private Runnable all=new Runnable() {

public void run() {
try{
SAXParserFactory sFactory=SAXParserFactory.newInstance();
SAXParser sParser=sFactory.newSAXParser();
XMLReader xmlReader=sParser.getXMLReader();
px=new ParsingXml(tagName,length);
xmlReader.setContentHandler(px);
URL url=new URL(str);
url.openConnection();
xmlReader.parse(new InputSource(url.openStream()));
}
catch(Exception e){
e.getMessage();Error=true;
}
handler.sendEmptyMessage(0);
}
};
ParsingXml px;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try{
if(length==0){
Error=true;
}else if(Error){
length=0;
}
if(Error){
length=0;
}
switch(length){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:DealsAdapter da=new DealsAdapter(act, px.store[0],px.store[1],px.store[2],px.store[3]);
      listview.setAdapter(da);
break;
case 5:
break;
case 0: ShowDialogBox("Error has occurred unknown reason");
}
pd.dismiss();
}catch(Exception e){
e.getMessage();
}

}
};
private void ShowDialogBox(String msg){
Builder builder=new AlertDialog.Builder(act);
builder.setTitle("Alerting");
builder.setMessage(msg);
builder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
}



0 comments:

Post a Comment