Tuesday, 2 October 2012

Student Handler


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;
 }

}

No comments:

Post a Comment