Friday, June 22, 2012

Sax Parser example in Android

There are 2 ways to parse an xml in android. SAX an DOM. The DOM parser loads the whole document into memory before it can work with it, which can be slow and uses up a lot more memory - the benefit is that you're not writing as much code. So in mobile environment we are not using commonly this method for parsing xml.But in  SAX , it goes through each element and attribute one at a time, and you can pick and choose which one you want added into memory, but you do need to write a lot more code.  So this one is used in android for parsing. Here find an simple example for implementing this.
 
public static Login parse(String xml) throws Exception {
 LoginHandler handler = new LoginHandler();
 Xml.parse(xml ,handler);
 return handler.tempItem;
}

public static class LoginHandler extends DefaultHandler {

  private static final String RESPONSE = "response";
  private static final String STATUS = "result";
  private static final String CSRF = "_csrf";
  private static final String USER_TOKEN = "user_token"; 

  Login tempItem;
  private StringBuilder builder;

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

                // taking upto </tag> position.
  @Override
  public void endElement(String uri, String localName, String qName)
    throws SAXException {
   super.endElement(uri, localName, qName);
   if (this.tempItem != null) {
    if (localName.equalsIgnoreCase(STATUS)) {
      tempItem.setStatus(builder.toString().trim());
    }
   }
    builder.setLength(0);
  }

  @Override
  public void error(SAXParseException e) throws SAXException {
   super.error(e);
  }

  @Override
  public void fatalError(SAXParseException e) throws SAXException {
   super.fatalError(e);
  }

  @Override
  public void startDocument() throws SAXException {
   super.startDocument();
   builder = new StringBuilder();
  }
                // taking start position <response _csrf="456465" user_token="4557897">
  @Override
  public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {
   builder.setLength(0);
   super.startElement(uri, localName, qName, attributes);
   if (localName.equalsIgnoreCase(RESPONSE)) {
       tempItem = new Login();
                            // taking attributes value    
                            tempItem.setCsrf(attributes.getValue(CSRF));
                            tempItem.setUser_token(attributes.getValue(USER_TOKEN));
   }
  }

 }

public class Login {

 private String csrf = "";
 public String getUser_token() {
  return user_token;
 }

 public void setUser_token(String user_token) {
  this.user_token = user_token;
 }

 private String status = "";
 private String user_token = "";

 public String getCsrf() {
  return csrf;
 }

 public void setCsrf(String csrf) {
  this.csrf = csrf;
 }

 public String getStatus() {
  return status;
 }

 public void setStatus(String status) {
  this.status = status;
 }

}

No comments:

Post a Comment