1

我尝试从 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 个不同的日子读取单独的数据?

4

2 回答 2

1

您所要做的就是循环预测时间

@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"}}
于 2013-08-19T20:01:06.877 回答
1

由于您有 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"}}
于 2013-08-19T20:56:04.843 回答