MainActivity.java
============
package com.android.parser;
import java.net.URL;
import java.util.ArrayList;
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;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ArrayList<MapItem> itemList=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemList=new ArrayList<MapItem>();
getData();
}
private void getData()
{
try
{
URL url=new URL("http://idrink4free.com/idrink4free/www/beer/map_trick.php");
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser saxparser=factory.newSAXParser();
XMLReader xmlReader=saxparser.getXMLReader();
Parser parser=new Parser(itemList);
xmlReader.setContentHandler(parser);
InputSource is = new InputSource(url.openStream());
xmlReader.parse(is);
for(MapItem i:itemList)
{
Log.i("Description","."+i.description);
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
MapItem.java
=========
package com.android.parser;
public class MapItem
{
String id,name,description,latitude,longitude,comment;
}
Parser.java
========
package com.android.parser;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Parser extends DefaultHandler {
private final static String TAG_ITEM = "tricklocation";
private final static String[] xmltags = { "id", "name", "description",
"latitude", "longitude", "comment" };
private MapItem mapItenm = null;
private ArrayList<MapItem> itemList = null;
private int curentIndex = -1;
private boolean isParsing = false;
private StringBuilder builder = new StringBuilder();
public Parser(ArrayList<MapItem> itemList) {
super();
this.itemList = itemList;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (localName.equalsIgnoreCase(TAG_ITEM)) {
mapItenm = new MapItem();
curentIndex = -1;
isParsing = true;
itemList.add(mapItenm);
} else {
curentIndex = itemIndexFromString(localName);
builder = null;
if (-1 != curentIndex)
builder = new StringBuilder();
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (localName.equalsIgnoreCase(TAG_ITEM)) {
isParsing = false;
} else if (curentIndex != -1) {
if(isParsing)
{
switch(curentIndex)
{
case 0: mapItenm.id = builder.toString(); break;
case 1: mapItenm.name = builder.toString(); break;
case 2: mapItenm.description = builder.toString();
break;
case 3: mapItenm.latitude= builder.toString(); break;
case 4: mapItenm.longitude= builder.toString(); break;
case 5: mapItenm.comment= builder.toString(); break;
}
}
}
}
public void characters(char[] chars, int start, int length)
throws SAXException {
super.characters(chars, start, length);
if (isParsing && -1 != curentIndex && null != builder) {
builder.append(chars, start, length);
}
}
private int itemIndexFromString(String tagname) {
int itemindex = -1;
for (int index = 0; index < xmltags.length; ++index) {
if (tagname.equalsIgnoreCase(xmltags[index])) {
itemindex = index;
break;
}
}
return itemindex;
}
}
============
package com.android.parser;
import java.net.URL;
import java.util.ArrayList;
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;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ArrayList<MapItem> itemList=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemList=new ArrayList<MapItem>();
getData();
}
private void getData()
{
try
{
URL url=new URL("http://idrink4free.com/idrink4free/www/beer/map_trick.php");
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser saxparser=factory.newSAXParser();
XMLReader xmlReader=saxparser.getXMLReader();
Parser parser=new Parser(itemList);
xmlReader.setContentHandler(parser);
InputSource is = new InputSource(url.openStream());
xmlReader.parse(is);
for(MapItem i:itemList)
{
Log.i("Description","."+i.description);
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
MapItem.java
=========
package com.android.parser;
public class MapItem
{
String id,name,description,latitude,longitude,comment;
}
Parser.java
========
package com.android.parser;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Parser extends DefaultHandler {
private final static String TAG_ITEM = "tricklocation";
private final static String[] xmltags = { "id", "name", "description",
"latitude", "longitude", "comment" };
private MapItem mapItenm = null;
private ArrayList<MapItem> itemList = null;
private int curentIndex = -1;
private boolean isParsing = false;
private StringBuilder builder = new StringBuilder();
public Parser(ArrayList<MapItem> itemList) {
super();
this.itemList = itemList;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (localName.equalsIgnoreCase(TAG_ITEM)) {
mapItenm = new MapItem();
curentIndex = -1;
isParsing = true;
itemList.add(mapItenm);
} else {
curentIndex = itemIndexFromString(localName);
builder = null;
if (-1 != curentIndex)
builder = new StringBuilder();
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
if (localName.equalsIgnoreCase(TAG_ITEM)) {
isParsing = false;
} else if (curentIndex != -1) {
if(isParsing)
{
switch(curentIndex)
{
case 0: mapItenm.id = builder.toString(); break;
case 1: mapItenm.name = builder.toString(); break;
case 2: mapItenm.description = builder.toString();
break;
case 3: mapItenm.latitude= builder.toString(); break;
case 4: mapItenm.longitude= builder.toString(); break;
case 5: mapItenm.comment= builder.toString(); break;
}
}
}
}
public void characters(char[] chars, int start, int length)
throws SAXException {
super.characters(chars, start, length);
if (isParsing && -1 != curentIndex && null != builder) {
builder.append(chars, start, length);
}
}
private int itemIndexFromString(String tagname) {
int itemindex = -1;
for (int index = 0; index < xmltags.length; ++index) {
if (tagname.equalsIgnoreCase(xmltags[index])) {
itemindex = index;
break;
}
}
return itemindex;
}
}