SAX (Simple API for XML) is an event based sequential access parser API which provides mechanism for reading data from an XML document. This parser is used as alternative for DOM (Document Object Model) which operates on the document as a whole, where as SAX parsers operate on each piece of the XML document sequentially.
Here I am going to use this SAX Parser API to get some information from the server using Wi-fi Connectivity and displaying the value on Tablet/Phone powered by Android 2.x.
Design of Application
Here I am going to use this SAX Parser API to get some information from the server using Wi-fi Connectivity and displaying the value on Tablet/Phone powered by Android 2.x.
Design of Application
In this Application there is no much needed any sorts of decorations and here we use just a label to display the parsed value
<?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/DisplayValue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
Main Activity
This class displays parsed value retrieved from URL and this class will throw exception if there is no net connectivity So add this module in this activity to check for internet connectivity
public boolean hasInternet(Context con) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) con
.getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected())
return false;
return true;
}
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) con
.getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected())
return false;
return true;
}
package com.parsesample.parser;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ParsingXML extends Activity {
/** Called when the activity is first created. */
TextView parseValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parseValue=new TextView(this);
try {
URL url = new URL("http://xxx.yyy.zzz.abc:qwe/yyy.xml");
SAXParserFactory saxFactory=SAXParserFactory.newInstance();
SAXParser parser=saxFactory.newSAXParser();
XMLReader reader=parser.getXMLReader();
XMLHandler handler=new XMLHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(url.openStream()));
ParseDataSet dataSet=handler.getParsedData();
parseValue.setText(dataSet.toString());
} catch (Exception e) {
//parseValue.setText("Error: "+e.getMessage());
Log.e("Exception Caught", e.getMessage());
}
setContentView(parseValue);
}
}
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ParsingXML extends Activity {
/** Called when the activity is first created. */
TextView parseValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parseValue=new TextView(this);
try {
URL url = new URL("http://xxx.yyy.zzz.abc:qwe/yyy.xml");
SAXParserFactory saxFactory=SAXParserFactory.newInstance();
SAXParser parser=saxFactory.newSAXParser();
XMLReader reader=parser.getXMLReader();
XMLHandler handler=new XMLHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(url.openStream()));
ParseDataSet dataSet=handler.getParsedData();
parseValue.setText(dataSet.toString());
} catch (Exception e) {
//parseValue.setText("Error: "+e.getMessage());
Log.e("Exception Caught", e.getMessage());
}
setContentView(parseValue);
}
}
Parser Class
This class handles parsing functionality of data from XML
/**
*
*/
package com.parsesample.parser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author venkat
*
*/
public class XMLHandler extends DefaultHandler {
protected boolean in_outertag = false;
protected boolean in_innertag = false;
protected boolean in_mytag = false;
protected boolean version = false;
private ParseDataSet dataSet = new ParseDataSet();
public ParseDataSet getParsedData() {
return this.dataSet;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
this.dataSet = new ParseDataSet();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("outertag")) {
this.in_outertag = true;
} else if (localName.equals("innertag")) {
this.in_innertag = true;
} else if (localName.equals("mytag")) {
this.in_mytag = true;
} else if (localName.equals("currentversion")) {
this.version=true;
} else if (localName.equals("tagwithnumber")) {
String atrValue = attributes.getValue("thenumber");
Integer i = Integer.parseInt(atrValue);
dataSet.setExtractedInt(i);
}
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("outertag")) {
this.in_outertag = false;
} else if (localName.equals("innertag")) {
this.in_innertag = false;
} else if (localName.equals("mytag")) {
this.in_mytag = false;
} else if (localName.equals("currentversion")) {
this.version=false;
}
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if (this.in_mytag) {
// dataSet.setExtractedString(new String(ch,start,length));
dataSet.setExtractedString(new String(ch));
} else if (this.version) {
dataSet.setExtractedString(new String(ch));
}
super.characters(ch, start, length);
}
}
*
*/
package com.parsesample.parser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author venkat
*
*/
public class XMLHandler extends DefaultHandler {
protected boolean in_outertag = false;
protected boolean in_innertag = false;
protected boolean in_mytag = false;
protected boolean version = false;
private ParseDataSet dataSet = new ParseDataSet();
public ParseDataSet getParsedData() {
return this.dataSet;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
this.dataSet = new ParseDataSet();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("outertag")) {
this.in_outertag = true;
} else if (localName.equals("innertag")) {
this.in_innertag = true;
} else if (localName.equals("mytag")) {
this.in_mytag = true;
} else if (localName.equals("currentversion")) {
this.version=true;
} else if (localName.equals("tagwithnumber")) {
String atrValue = attributes.getValue("thenumber");
Integer i = Integer.parseInt(atrValue);
dataSet.setExtractedInt(i);
}
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("outertag")) {
this.in_outertag = false;
} else if (localName.equals("innertag")) {
this.in_innertag = false;
} else if (localName.equals("mytag")) {
this.in_mytag = false;
} else if (localName.equals("currentversion")) {
this.version=false;
}
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if (this.in_mytag) {
// dataSet.setExtractedString(new String(ch,start,length));
dataSet.setExtractedString(new String(ch));
} else if (this.version) {
dataSet.setExtractedString(new String(ch));
}
super.characters(ch, start, length);
}
}
Parser Data Set
This POJO handles parser value and returns to Handler class
package com.parsesample.parser;
import android.util.Log;
public class ParseDataSet {
private String extractedString = null;
private int extractedInt = 0;
public String getExtractedString() {
return extractedString;
}
public void setExtractedString(String extractedString) {
this.extractedString = extractedString;
}
public int getExtractedInt() {
return extractedInt;
}
public void setExtractedInt(int extractedInt) {
this.extractedInt = extractedInt;
}
public String toString(){
Log.d("Extracted String ->>>>", this.extractedString);
/* return "ExtractedString = " + this.extractedString
+ "nExtractedInt = " + this.extractedInt;*/
return this.extractedString;
}
}
import android.util.Log;
public class ParseDataSet {
private String extractedString = null;
private int extractedInt = 0;
public String getExtractedString() {
return extractedString;
}
public void setExtractedString(String extractedString) {
this.extractedString = extractedString;
}
public int getExtractedInt() {
return extractedInt;
}
public void setExtractedInt(int extractedInt) {
this.extractedInt = extractedInt;
}
public String toString(){
Log.d("Extracted String ->>>>", this.extractedString);
/* return "ExtractedString = " + this.extractedString
+ "nExtractedInt = " + this.extractedInt;*/
return this.extractedString;
}
}