public class StudentHandler extends DefaultHandler
{
public static Vector<Student> vecStudent;
Student student;
StringBuffer currentString;
//boolean currentElement = false;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//currentElement = false;
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 {
//currentElement = true;
if(localName.equalsIgnoreCase("name"))
{
Log.e("name",currentString.toString());
student.name = currentString.toString();
}
else if(localName.equalsIgnoreCase("sno"))
{
Log.e("sno",currentString.toString());
student.sno = currentString.toString();
}
else if(localName.equalsIgnoreCase("city"))
{
Log.e("city",currentString.toString());
student.city = currentString.toString();
}
else if(localName.equalsIgnoreCase("Student"))
{
vecStudent.add(student);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// if(currentElement)
currentString.append(ch,start,length);
}
public Vector<Student> getData()
{
return vecStudent;
}
}
Tuesday, 2 October 2012
Student Handler
Sax Parser Student
package com.parsers;
public class Student {
String name;
String sno;
String city;
}
Sax Parser Data Handler
public class DataHandler
{
private SQLiteDatabase sqLiteDatabase;
MyHelper myHelper;
Context context;
public DataHandler(Context context) {
myHelper = new MyHelper(context, "College.sqlite", null,1);
}
public class MyHelper extends SQLiteOpenHelper
{
public MyHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table Student(sno text,sname text ,city text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public void openDataBase()
{
sqLiteDatabase = myHelper.getWritableDatabase();
}
public void insertStudent(Vector<Student> vecStudent)
{
openDataBase();
Student student;
ContentValues contentValues = null;
for(int i = 0;vecStudent.size()>0;i++)
{
student = vecStudent.get(0);
contentValues = new ContentValues();
contentValues.put("sno", student.sno);
contentValues.put("name", student.name);
contentValues.put("city", student.city);
sqLiteDatabase.insert("Student", "", contentValues);
}
}
public void closeDataBase()
{
sqLiteDatabase.close();
}
}
Sax Parsing
public class ParsersDemo extends Activity {
ListView lvList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvList = (ListView) findViewById(R.id.lvList);
SAXParser saxParser;
try {
saxParser = SAXParserFactory.newInstance().newSAXParser();
saxParser.parse(getResources().openRawResource(R.raw.test),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();
}
DataHandler dataHandler = new DataHandler(getApplicationContext());
dataHandler.insertStudent(new StudentHandler().getData());
dataHandler.closeDataBase();
lvList.setAdapter(new MyAdapter(new StudentHandler().getData()));
}
public class MyAdapter extends BaseAdapter
{
Vector<Student> vecStudent;
MyAdapter(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 null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Student student = vecStudent.get(position);
convertView = (LinearLayout)getLayoutInflater().inflate(R.layout.list_cell, null);
TextView tvsno = (TextView) convertView.findViewById(R.id.sno);
TextView tvsname = (TextView) convertView.findViewById(R.id.sname);
TextView tvcity = (TextView) convertView.findViewById(R.id.city);
tvsno.setText(student.sno);
tvsname.setText(student.name);
tvcity.setText(student.city);
return convertView;
}
}
}
Subscribe to:
Comments (Atom)