Hi! I am totally new to using a Sax parser! Can anyone help me understand how to grab my xml content and store each group in an array list for later access. Within the XML there is repeating groups of information for different days and I want to be able to grab each day's info and store it in a java class. Here is my sax parser and it is reading all of the XML tags.
package com.example.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.util.Log;
import com.example.test.WeatherData.Forecast;
public class SaxParserWeather extends DefaultHandler{
private final String DEB_TAG = "SAXParser.java";
public WeatherData weatherData;
private String tempVal;
private boolean local;
private boolean day;
private List<String> tagTree;
private int tagLevel = -1;
private Forecast forecast;
//to maintain context
private Context mContext;
public SaxParserWeather(Context context){
tagTree = new ArrayList<String>();
mContext = context;
weatherData = new WeatherData();
local = false;
day = false;
}
public void runExample() {
parseDocument();
}
private void parseDocument() {
//get a factory
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
Log.d(DEB_TAG, "in try");
//get a new instance of parser
SAXParser sp = spf.newSAXParser();
//parse the file and also register this class for call backs
InputStream is = mContext.getResources().openRawResource(R.raw.awfeed);
sp.parse(is, this);
}catch(SAXException se) {
se.printStackTrace();
Log.d(DEB_TAG, "catch1");
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
Log.d(DEB_TAG, "catch2");
}catch (IOException ie) {
ie.printStackTrace();
Log.d(DEB_TAG, "catch3");
}
}
//Event Handlers
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//reset
Log.d(DEB_TAG, "Inside startElement at localName: " + localName);
tempVal = "";
tagLevel++;
tagTree.add(localName.trim());
if(localName.equalsIgnoreCase("local")) {
}else if(localName.equalsIgnoreCase("currentconditions")) {
Log.d(DEB_TAG, "Setting local to true");
local = true;
}else if(localName.equalsIgnoreCase("day")){
Log.d(DEB_TAG, "Setting day to true");
day = true;
forecast = new WeatherData().createForecast();
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if(localName.equalsIgnoreCase("city")) {
weatherData.location.city = tempVal;
}else if (localName.equalsIgnoreCase("state")) {
weatherData.location.state = tempVal;
}else if (localName.equalsIgnoreCase("time")) {
weatherData.location.time = tempVal;
}
//Log.d(DEB_TAG, "in end: local = " + local + " tag = " + localName);
// Current
if(tagLevel == 2 && tagTree.get(tagLevel - 1).equals("currentconditions")){
if (localName.equalsIgnoreCase("temperature") && local) {
weatherData.current.temp = tempVal;
local = false;
}else if (localName.equalsIgnoreCase("realfeel")) {
weatherData.current.realfeel = tempVal;
}else if (localName.equalsIgnoreCase("humidity")) {
weatherData.current.humidity = tempVal;
}
}
// Forecast
if(tagLevel == 3 && tagTree.get(tagLevel - 1).equals("day")){
if(localName.equalsIgnoreCase("daycode") && day){
weatherData.forecast.add(forecast);
day = true;
}else if (localName.equalsIgnoreCase("daycode")){
forecast.day = tempVal;
Log.d(DEB_TAG, "Vale of daycode is " + tempVal);
}else if (localName.equalsIgnoreCase("weathericon")){
forecast.icon_d = tempVal;
}else if (localName.equalsIgnoreCase("hightemperature")){
forecast.high_d = tempVal;
}else if (localName.equalsIgnoreCase("lowtemperature")){
forecast.low_d = tempVal;
}
}
tagTree.remove(tagTree.size() - 1);
tagLevel--;
}
}
So if the tag is equal to "day" I want to call my "createforecast" class and populate an arraylist with the tag data.
public class WeatherData {
//instance data
private final String DEB_TAG = "WeatherData.java";
public Location location;
public Current current;
public List<Forecast> forecast;
public WeatherData(){
location = new Location();
current = new Current();
forecast = new ArrayList<Forecast>();
}
public class Forecast{
public String day;
public String icon_d;
public String high_d;
public String low_d;
public Forecast(){}
public Forecast(String day, String icon_d, String high_d, String low_d){
this.day = day;
this.icon_d = icon_d;
this.high_d = high_d;
this.low_d = low_d;
Log.d("DEB_TAG", "Value of day " + day);
}
}
public Forecast createForecast(){
//forecast.add()
Log.d("DEB_TAG", "inside createForecast");
return new Forecast();
}
}
If anyone has a suggestion to a different way to grab each days worth of info...please, I welcome any help!