试图了解Android的Sax Parser

时间:2010-07-22 13:12:54

标签: android sax

我使用过Java Sax解析器,但我正在尝试用这个XML实现Android。

<?xml version="1.0"  encoding="utf-8"?>
<adc_database xmlns="http://www.weather.com">
    <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</url>


            </watchwarnareas>

    <currentconditions daylight="True">

            <url>http://www.weather.com</url>

            <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"/>
        </mapSpace>

        <forecast>

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


                <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;fday=2</url>

                <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>

我将从此网址(http://www.ibm.com/developerworks/opensource/library/x-android/index.html

离开示例

我不确定如何在XML中抓取不同的级别。我有一个WeatherData类,它存储了我需要的所有信息:

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();
}

}

我不理解的是如何进入我的XML的不同层次。我有我的根标签adc_database,其中包含我的LOCAL,CURRENTCONDITIONS和FORECAST的开启和关闭,其中FORECAST有多个每日预测。到目前为止,这是我的代码......

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;
    }
}

如果有人可以帮助解释SAX代码以及我将如何进入不同的级别,或者可以指向另一个比一个级别更深的例子会很棒!

1 个答案:

答案 0 :(得分:0)

  

我不理解的是如何   步入我的不同层次   XML

想要了解更好的方法,但目前我的方法只是解析一次作为单个文档,使用多个getChild的嵌套级别。
即:

item.getChild(LOCAL).getChild(ADMINAREA).setEndTextElementListener(new EndTextElementListener(){