我尝试从XML文件读取openweathermap数据
XML代码:
<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 is 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 is clear" all="0" unit="%"/>
</time>
</forecast>
</weatherdata>
我用这个代码来读取这个
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
当我读了一天的数据,但当我尝试读这个时,它就起作用了
<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 is 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 is clear" all="0" unit="%"/>
</time>
<time day="2013-08-20">
<symbol number="800" name="sky is 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 is 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>
它读取数据三次
如何使用nokogiri读取3个不同日期的单独数据?
您所要做的就是在预测时间上循环
@forcasts = {}
doc.xpath('/weatherdata/forecast/time').each do |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
循环后,@forcasts
将拥有如下所有数据:
{"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"}}
由于您有3天的数据,因此要获得所有数据,您可以使用以下方法:
time_data = {}
doc.xpath("//time").each do |node|
key = node.xpath("@day").text
time_data[key] ||= {}
time_data[key]['symbol'] ||= node.xpath("symbol/@name").text
time_data[key]['windname'] ||= node.xpath("windSpeed/@name").text
time_data[key]['mps'] ||= node.xpath("windSpeed/@mps").text
#.......
end
这将使time_data为:
{"2013-08-19"=>{"symbol"=>"sky is clear", "windname"=>"Light breeze", "mps"=>"3.25"}, "2013-08-20"=>{"symbol"=>"sky is clear", "windname"=>"Gentle Breeze", "mps"=>"4.31"}, "2013-08-21"=>{"symbol"=>"moderate rain", "windname"=>"", "mps"=>"1.51"}}