ruby - rails parsing xml data from openweathermap -


i try read openweathermap data xml file

xml code:

<weatherdata> <location> <name>tokyo</name> <type/> <country>jp</country> <timezone/> <location altitude="0" latitude="35.689499" longitude="139.691711" geobase="geonames" geobaseid="0"/> </location> <credit/> <meta> <lastupdate>2013-08-19t19:30:49</lastupdate> <calctime>0.0119</calctime> <nextupdate>2013-08-19t22:30:49</nextupdate> </meta> <sun rise="2013-08-18t20:03:32" set="2013-08-19t09:26:02"/> <forecast> <time day="2013-08-19"> <symbol number="800" name="sky clear" var="01n"/> <precipitation/> <winddirection deg="197" code="ssw" name="south-southwest"/> <windspeed mps="3.25" name="light breeze"/> <temperature day="27.43" min="27.43" max="27.43" night="27.43" eve="27.43" morn="27.43"/> <pressure unit="hpa" value="1012.64"/> <humidity value="82" unit="%"/> <clouds value="sky clear" all="0" unit="%"/> </time> </forecast> </weatherdata> 

and use code read this

doc = nokogiri::xml(open("http://api.openweathermap.org/data/2.5/forecast/daily?q=tokyo&mode=xml&units=metric&cnt=1"))         doc.remove_namespaces!           @city = doc.xpath('//name').inner_text         @country = doc.xpath('//country').inner_text         @lastupdate = doc.xpath('//lastupdate').inner_text         @symbolvar = doc.xpath('//symbol/@var').inner_text          @temperature = doc.xpath('//temperature/@day').inner_text         @temperaturemin = doc.xpath('//temperature/@min').inner_text         @temperaturemax = doc.xpath('//temperature/@max').inner_text          @symbol = doc.xpath('//symbol/@name').inner_text         @windname = doc.xpath('//windspeed/@name').inner_text          @mps = doc.xpath('//windspeed/@mps').inner_text          @winddirection = doc.xpath('//winddirection/@name').inner_text          @winddirectiondeg = doc.xpath('//winddirection/@deg').inner_text           @clouds = doc.xpath('//clouds/@value').inner_text           @pressure = doc.xpath('//pressure/@value').inner_text 

its works when read data 1 day when try read

<weatherdata> <location> <name>tokyo</name> <type/> <country>jp</country> <timezone/> <location altitude="0" latitude="35.689499" longitude="139.691711" geobase="geonames" geobaseid="0"/> </location> <credit/> <meta> <lastupdate>2013-08-19t19:36:46</lastupdate> <calctime>0.0241</calctime> <nextupdate>2013-08-19t22:36:46</nextupdate> </meta> <sun rise="2013-08-18t20:03:32" set="2013-08-19t09:26:02"/> <forecast> <time day="2013-08-19"> <symbol number="800" name="sky clear" var="01n"/> <precipitation/> <winddirection deg="197" code="ssw" name="south-southwest"/> <windspeed mps="3.25" name="light breeze"/> <temperature day="27.43" min="27.43" max="27.43" night="27.43" eve="27.43" morn="27.43"/> <pressure unit="hpa" value="1012.64"/> <humidity value="82" unit="%"/> <clouds value="sky clear" all="0" unit="%"/> </time> <time day="2013-08-20"> <symbol number="800" name="sky clear" var="01d"/> <precipitation/> <winddirection deg="191" code="s" name="south"/> <windspeed mps="4.31" name="gentle breeze"/> <temperature day="35.02" min="27.36" max="35.62" night="28.63" eve="33.04" morn="27.36"/> <pressure unit="hpa" value="1012.26"/> <humidity value="58" unit="%"/> <clouds value="sky clear" all="0" unit="%"/> </time> <time day="2013-08-21"> <symbol number="501" name="moderate rain" var="10d"/> <precipitation value="11" type="rain"/> <winddirection deg="87" code="e" name="east"/> <windspeed mps="1.51" name=""/> <temperature day="33.7" min="25.37" max="33.7" night="25.37" eve="27.07" morn="26.67"/> <pressure unit="hpa" value="1014.26"/> <humidity value="64" unit="%"/> <clouds value="few clouds" all="24" unit="%"/> </time> </forecast> </weatherdata> 

it's read data 3 times

how use nokogiri read separate data 3 different days?

all have loop on forecast times

@forcasts = {} doc.xpath('/weatherdata/forecast/time').each |forcast|   day = {}   day['day'] = forcast.xpath('./temperature/@day').inner_text   day['min'] = forcast.xpath('./temperature/@min').inner_text   day['max'] = forcast.xpath('./temperature/@max').inner_text   date = forcast.attr('day').inner_text   @forcasts[ date ] = day end 

after loop @forcasts have data this:

{"2013-08-19"=>{"day"=>"27.43", "min"=>"27.43", "max"=>"27.43"},  "2013-08-20"=>{"day"=>"35.02", "min"=>"27.36", "max"=>"35.62"},  "2013-08-21"=>{"day"=>"33.7", "min"=>"25.37", "max"=>"33.7"}} 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -