tags:

views:

82

answers:

0

I have used the Java Sax parser, but am trying to implement Android's with this XML.

<?xml version="1.0"  encoding="utf-8"?>
<adc_database xmlns="http://www.weather.com"&gt;
    <units>
        <temp>F</temp>
        <dist>MI</dist>
        <speed>MPH</speed>
        <pres>IN</pres>

        <prec>IN</prec>
    </units>

<local>
<city>State College</city> 
<adminArea>Pennsylvania</adminArea> 
    <country code="US">United States</country>
<lat>40.803</lat> 
<lon>-77.894</lon> 
<time>14:35</time> 
<timeZone>-5</timeZone>

<obsDaylight>
1
</obsDaylight>  
</local>

            <watchwarnareas zone="PAZ019" county="PAC027" isactive="1">

                    <warningtype>SEVERE THUNDERSTORM WATCH</warningtype>

                <url>http://www.weather.com/watches-warnings.asp&lt;/url&gt;


            </watchwarnareas>

    <currentconditions daylight="True">

            <url>http://www.weather.com&lt;/url&gt;

            <observationtime>2:35 PM</observationtime>

            <temperature>84</temperature>

            <realfeel>90</realfeel>


            <humidity>61%</humidity>

            <weathericon>07</weathericon>

            <windgusts>16</windgusts>

            <windspeed>8</windspeed>

            <winddirection>WNW</winddirection>

            <visibility>10</visibility>



    </currentconditions>


        <mapSpace>
        <image resolution="480x480" url="http://vortex.png"/&gt;
        </mapSpace>

        <forecast>

                <url>http://www.weather.com/forecast.asp&lt;/url&gt;
                <day number="1">
                        <url>http://www.weather.com/&lt;/url&gt;


                <obsdate>7/21/2010</obsdate>
                <daycode>Wednesday</daycode>

                    <sunrise>5:59 AM</sunrise>
                    <sunset>8:37 PM</sunset>

                <daytime>

                        <weathericon>17</weathericon>


                        <hightemperature>85</hightemperature>
                        <lowtemperature>63</lowtemperature>

                        <realfeelhigh>93</realfeelhigh>
                        <realfeellow>65</realfeellow>

                        <windspeed>8</windspeed>

                        <winddirection>W</winddirection>


                        <windgust>32</windgust>

                        <rainamount>0.01</rainamount>

                        <snowamount>0.0</snowamount>

                        <iceamount>0.00</iceamount>

                        <tstormprob>91</tstormprob>

                </daytime>

                <nighttime>

                        <weathericon>35</weathericon>

                        <hightemperature>85</hightemperature>
                        <lowtemperature>63</lowtemperature>

                        <realfeelhigh>93</realfeelhigh>
                        <realfeellow>65</realfeellow>


                        <windspeed>4</windspeed>

                        <winddirection>W</winddirection>

                        <windgust>10</windgust>

                        <rainamount>0.20</rainamount>

                        <snowamount>0.0</snowamount>

                        <iceamount>0.00</iceamount>


                        <tstormprob>24</tstormprob>

                </nighttime>
            </day>
            <day number="2">
                        <url>http://www.weather.com&amp;amp;fday=2&lt;/url&gt;

                <obsdate>7/22/2010</obsdate>

                <daycode>Thursday</daycode>

                    <sunrise>6:00 AM</sunrise>
                    <sunset>8:37 PM</sunset>

                <daytime>

                        <weathericon>02</weathericon>

                        <hightemperature>86</hightemperature>

                        <lowtemperature>63</lowtemperature>

                        <realfeelhigh>87</realfeelhigh>
                        <realfeellow>65</realfeellow>

                        <windspeed>8</windspeed>

                        <winddirection>W</winddirection>

                        <windgust>19</windgust>


                        <rainamount>0.00</rainamount>

                        <snowamount>0.0</snowamount>

                        <iceamount>0.00</iceamount>

                        <tstormprob>0</tstormprob>

                </daytime>
                <nighttime>

                        <weathericon>42</weathericon>


                        <hightemperature>86</hightemperature>
                        <lowtemperature>63</lowtemperature>

                        <realfeelhigh>87</realfeelhigh>
                        <realfeellow>65</realfeellow>

                        <windspeed>2</windspeed>

                        <winddirection>N</winddirection>


                        <windgust>8</windgust>

                        <rainamount>0.00</rainamount>

                        <snowamount>0.0</snowamount>

                        <iceamount>0.00</iceamount>

                        <tstormprob>40</tstormprob>

                </nighttime>

            </day>

I am going off of the example at this URL (http://www.ibm.com/developerworks/opensource/library/x-android/index.html

I am not sure how I grab different levels within the XML. I have a WeatherData class that stores all the info that I need:

public class WeatherData {
//instance data
@SuppressWarnings("unused")
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 Location {
    public String time;
    public String city;
    public String state;

    public Location(){

    }
    public Location(String time, String city, String state){
        this.time   = time;
        this.city   = city;
        this.state  = state;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

public class Current {
    public String temp;
    public String realfeel;
    public String humidity;
    public String icon;

    public String getTemp() {
        return temp;
    }
    public void setTemp(String temp) {
        this.temp = temp;
    }
    public String getRealfeel() {
        return realfeel;
    }
    public void setRealfeel(String realfeel) {
        this.realfeel = realfeel;
    }
    public String getHumidity() {
        return humidity;
    }
    public void setHumidity(String humidity) {
        this.humidity = humidity;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }


    public Current(){

    }
    public Current(String temp, String realfeel, String humidity, String icon){
        this.temp       = temp;
        this.realfeel   = realfeel;
        this.humidity   = humidity;
        this.icon       = icon;
        Log.d("DEB_TAG", "Value of temp inside Current is " + temp);
    }
}

public class Forecast{
    // Forecast member variables
    public String day_d;
    public String icon_d;
    public String high_d;
    public String low_d;
    public String obsDate_d;

    public String getDay_d() {
        return day_d;
    }
    public void setDay_d(String dayD) {
        day_d = dayD;
    }
    public String getIcon_d() {
        return icon_d;
    }
    public void setIcon_d(String iconD) {
        icon_d = iconD;
    }
    public String getHigh_d() {
        return high_d;
    }
    public void setHigh_d(String highD) {
        high_d = highD;
    }
    public String getLow_d() {
        return low_d;
    }
    public void setLow_d(String lowD) {
        low_d = lowD;
    }
    public String getObsDate_d() {
        return obsDate_d;
    }
    public void setObsDate_d(String obsDateD) {
        obsDate_d = obsDateD;
    }
}

public Forecast createForecast(){
    return new Forecast();
}

}

What I am not understanding is how to step into the different levels of my XML. I have my root tag adc_database that contains my opening and closings of LOCAL, CURRENTCONDITIONS, and FORECAST, where FORECAST has multiple daily forecasts. Here is my code so far...

public class AndroidSaxFeedParser extends BaseFeedParser {

    public AndroidSaxFeedParser(String feedUrl) {
        super(feedUrl);
    }

    public List<WeatherData> parse() {
        final WeatherData currentData       = new WeatherData();
        RootElement root                    = new RootElement("rss");
        final List<WeatherData> conditions  = new ArrayList<WeatherData>();
        Element adc_database                = root.getChild("adc_database");
        Element item                        =  adc_database.getChild(LOCAL);

        item.setEndElementListener(new EndElementListener(){
            public void end() {
                conditions.add(currentData.copy());
            }
        });
        item.getChild(CITY).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentData.location.setCity(body);
            }
        });
        item.getChild(ADMINAREA).setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                currentData.location.setState(body);
            }
        });

        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, 
root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return conditions;
    }
}

If anyone can help explain the SAX code and how I would go into the different levels or could point me to another example that goes deeper than one level would be great!