2010-07-22 4 views
1

Я использовал парсер Java Sax, но пытаюсь реализовать Android с этим XML.Попытка понять Android Sax Parser

<?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, который содержит мой O сближения и закрытия ЛОКАЛЬНЫХ, ТЕКУЩИХ СОСТОЯНИЙ и ПРОГНОЗИРОВАНИЯ, где ПРОГНОЗ имеет несколько ежедневных прогнозов. Вот мой код до сих пор ...

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 и как я пошел бы в разных уровнях или может указывать мне другой пример, который идет глубже, чем на один уровень было бы здорово!

ответ

0

То, что я не понимая, как на шаг в различных уровнях моего XML

Вы хотите знать, лучший способ, но в настоящее время мой подход просто разобрать один раз как один doc, с вложенными уровнями с использованием нескольких getChild.
i.e .:

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