tmv105 0 Newbie Poster

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.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 = "";
	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() {


	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) {
			Log.d(DEB_TAG, "catch1");
		}catch(ParserConfigurationException pce) {
			Log.d(DEB_TAG, "catch2");
		}catch (IOException ie) {
			Log.d(DEB_TAG, "catch3");


	//Event Handlers
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		Log.d(DEB_TAG, "Inside startElement at localName: " + localName);
		tempVal = "";
		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")) { = 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){
				day = true;
			}else if (localName.equalsIgnoreCase("daycode")){ = 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);

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 = "";
	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){ 	= 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(){
		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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.