Wednesday, January 23, 2013

Android parse Big(3MB) KML file



Android  parse Big(3MB) KML  file
Below I wrote the code for how to parse big KML files in android .
For parsing big KML file I am using a SAX parser
Why Sax parser?
*Parsing data speed better.
*Dom parser can’t parse a big file.
* Json also while parsing big json file too slow and some text are missing.
*So finally I happy with SaxParser .
 
Screen Shot



Activity Soure Code
This is activity class it contain user interface of app and setting adapter to Listview
package com.vj.parsingbigfile;
import java.io.IOException;
import java.util.List;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.widget.ListView;
import android.widget.ProgressBar;

/**
 *
 * @author VIJAYAKUMAR M
 *http://www.android-ever.com/
 */
public class MainActivity extends Activity {
     
        public static List<ParsingStructure> parsingStr = null;
        ListAdap listAdpter;
        ListView listview  = null;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listview = (ListView)findViewById(R.id.listView1);
            ParsingBigFileAsync parsingAsync = new ParsingBigFileAsync();
            parsingAsync.execute();
                       
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
     
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
      }

     
      public class ParsingBigFileAsync extends AsyncTask<String, Void , String>{
     String  result;
     Dialog dialog;
     ProgressBar pBar ;
            @Override
            public void onPreExecute(){
                   dialog = new Dialog(MainActivity.this);
                   dialog.setTitle("Loading......");
                   dialog.show();
            }
            @Override
            protected String doInBackground(String... params) {
                   try {
                         parsingStr = SAXXMLParser.parse(getAssets().open("values.kml"));
                            result = "in";
                        } catch (IOException e) {
                              e.printStackTrace();
                              result = "out";
                        }
                       
                  return result;
            }
           
            @Override
            public void onPostExecute(String result){
                  if(result.equalsIgnoreCase("in")){
                      listAdpter = new ListAdap(MainActivity.this, R.layout.listitemview, parsingStr);
                        listview.setAdapter(listAdpter);
                        dialog.dismiss();
                  }
            }
           
      }

}



Adapter Class

This is adapter class for set all values to TextView
package com.vj.parsingbigfile;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
 *
 * @author VIJAYAKUMAR M
 *http://www.android-ever.com/
 */
public class ListAdap  extends ArrayAdapter<ParsingStructure>{
      Context ctx;
      List<ParsingStructure> parsignStrList = null ;
      public ListAdap(Context context, int textViewResourceId,List<ParsingStructure> parsing) {
            super(context, textViewResourceId,parsing);
            ctx = context;
            parsignStrList = parsing;
      }
      @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View rowView = inflater.inflate(R.layout.listitemview, parent, false);
          TextView  text = (TextView)rowView.findViewById(R.id.textView1);
          try {
                    text.setText( parsignStrList.get(position).getName() );
         } catch (Exception e) {
            e.printStackTrace();
         }
          return rowView;
        }
}


ParsingStructure class
This class for creating structure for XML values
package com.vj.parsingbigfile;

/**
 *
 * @author VIJAYAKUMAR M
 * http://www.android-ever.com/
 */
public class ParsingStructure {
          private String name;
          private String description;
          private String coordinates;
          public String getName() {
              return name;
          }
       
          public void setName(String name) {
              this.name = name;
          }
       
          public String getDescription() {
              return description;
          }
       
          public void setDescription(String description) {
              this.description = description;
          }
       
          public String getCoordinates() {
              return coordinates;
          }
       
          public void setCoordinates(String coordinates) {
              this.coordinates = coordinates;
              if(this.name.trim().equalsIgnoreCase("Elimbah Creek (east)"))
                  System.out.println(coordinates);
          }
}





This class for handling xml  and setting the value to structure class
package com.vj.parsingbigfile;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXXMLHandler extends DefaultHandler {

    private List<ParsingStructure> parsingStructure;
    private StringBuilder tempVal;
    private ParsingStructure tempStr;

    public SAXXMLHandler() {
      parsingStructure = new ArrayList<ParsingStructure>();
    }

    public List<ParsingStructure> getParsingvalues() {
        return parsingStructure;
    }
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
      
        tempVal = new StringBuilder();
        if (qName.equalsIgnoreCase("Placemark")) {
            tempStr = new ParsingStructure();
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
      if (tempVal != null) {
            for (int i=start; i<start+length; i++) {
                  tempVal.append(ch[i]);
            }
        }
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equalsIgnoreCase("Placemark")) {
            // add it to the list
            parsingStructure.add(tempStr);
        } else if (qName.equalsIgnoreCase("name")) {
            if(tempStr != null)
                  tempStr.setName(tempVal.toString());
        } else if (qName.equalsIgnoreCase("description")) {
            if(tempStr != null)
                  tempStr.setDescription(tempVal.toString());
        } else if (qName.equalsIgnoreCase("coordinates")) {
            if(tempStr != null)
                  tempStr.setCoordinates(tempVal.toString());
        } else if(qName.equalsIgnoreCase("kml")) {
           
        }
    }
   
    public void warning(SAXParseException e) throws SAXException {
     
    }
   
    public void error(SAXParseException e) throws SAXException {
     
    }
   
    public void fatalError(SAXParseException e) throws SAXException {
   
    }
}


This class for SAX parsing(reading) from XML file .
package com.vj.parsingbigfile;

import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;


public class SAXXMLParser {
    public static List<ParsingStructure> parse(InputStream is) {
        List<ParsingStructure> parsingStru = null;
        try {
            // create a XMLReader from SAXParser
            XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
                    .getXMLReader();
            // create a SAXXMLHandler
            SAXXMLHandler saxHandler = new SAXXMLHandler();
            // store handler in XMLReader
            xmlReader.setContentHandler(saxHandler);
            // the process starts
            xmlReader.parse(new InputSource(is));
            // get the `get list`
            parsingStru = saxHandler.getParsingvalues();
        } catch (Exception ex) {
         
        }
        return parsingStru;
    }
}

1 comment:

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...