2010-07-22 3 views
1

J'ai utilisé l'analyseur Java Sax, mais j'essaie d'implémenter Android avec ce XML.Essayer de comprendre l'analyseur Sax d'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> 

Je partais de l'exemple à l'adresse (http://www.ibm.com/developerworks/opensource/library/x-android/index.html

Je ne sais pas comment je prends différents niveaux au sein du XML que j'ai une classe WeatherData qui stocke toutes les informations que j'ai besoin.:

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

}

ce que je ne comprends pas comment à l'étape dans les différents niveaux de mon XML. J'ai ma balise racine adc_database qui contient mon o Pening et fermetures de LOCAL, CURRENTCONDITIONS et PRÉVISION, où PRÉVISION a plusieurs prévisions quotidiennes. Voici mon code jusqu'à présent ...

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

Si quelqu'un peut aider à expliquer le code SAX et comment j'aller dans les différents niveaux ou pourrait me pointer vers un autre exemple qui va plus loin que d'un niveau serait génial!

Répondre

0

Ce que je ne comprends pas est comment pas dans les différents niveaux de mon XML

Voudrait savoir une meilleure façon, mais actuellement mon approche est juste pour analyser une fois en une seule doc, avec les niveaux imbriqués en utilisant plusieurs getChild.
i.e .:

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