Monday, 9 April 2012

XML parsing in Android


SAX PARSING


SAX is an abbreviation for “Simple API for XML”, and it is a very powerful tool for reading XML.


Advantages 
1. low memory footprint.
2. They parse each line of XML data at a time and consequently do not need to load the entire XML document into memory prior to making the data accessible.
3. This is a significant boost to performance, and this really becomes visible when working with large XML documents.

Disadvantages 
1. You must define an event-driven API that will respond to each element as it is received.
2. This can become time consuming to build, but if you are willing to spend a little extra time to get it right, the outcome will be worthwhile.



DOM PARSING

package com.xmlparse.android;

import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class XMLParsingActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     LinearLayout layout = new LinearLayout(this);
     layout.setOrientation(1);
     TextView name[];
     TextView website[];
     TextView category[];

     try {
          URL url = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc = db.parse(new InputSource(url.openStream()));
          doc.getDocumentElement().normalize();

          NodeList nodeList = doc.getElementsByTagName("item");
          name = new TextView[nodeList.getLength()];
          website = new TextView[nodeList.getLength()];
          category = new TextView[nodeList.getLength()];

          for (int i = 0; i < nodeList.getLength(); i++) {
               Node node = nodeList.item(i);
               name[i] = new TextView(this);
               website[i] = new TextView(this);
               category[i] = new TextView(this);
               Element fstElmnt = (Element) node;

               NodeList nameList = fstElmnt.getElementsByTagName("name");
               Element nameElement = (Element) nameList.item(0);
               nameList = nameElement.getChildNodes();
               name[i].setText("Name = " + ((Node) nameList.item(0)).getNodeValue());
               
               NodeList websiteList = fstElmnt.getElementsByTagName("website");
               Element websiteElement = (Element) websiteList.item(0);
               websiteList = websiteElement.getChildNodes();
               website[i].setText("Website = " + ((Node)                        websiteList.item(0)).getNodeValue());
               category[i].setText("Website Category = "                websiteElement.getAttribute("category"));
               
               layout.addView(name[i]);
               layout.addView(website[i]);
               layout.addView(category[i]);
          }
     } catch (Exception e) {
          System.out.println("XML Pasing Excpetion = " + e);
     }
     setContentView(layout);
}
}

No comments:

Post a Comment