Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 从XML文档获取元素文本_Ruby_Xml_Xpath_Nokogiri - Fatal编程技术网

Ruby 从XML文档获取元素文本

Ruby 从XML文档获取元素文本,ruby,xml,xpath,nokogiri,Ruby,Xml,Xpath,Nokogiri,我试图从地下天气的XML中提取一些信息 我可以打开资源并提取所需的元素,但我确实希望将元素text作为变量返回,而不包含XML元素标记,因此我可以对其进行操作并将其显示在网页上 也许有一种方法可以使用regexp去掉标记,但我怀疑/希望我可以直接在Nokogiri中以更优雅的方式完成这项工作 目前我正在使用irb计算语法: irb>require 'rubygems' irb>require 'nokogiri' irb>require 'open-uri' irb>do

我试图从地下天气的XML中提取一些信息

我可以打开资源并提取所需的元素,但我确实希望将元素
text
作为变量返回,而不包含XML元素标记,因此我可以对其进行操作并将其显示在网页上

也许有一种方法可以使用regexp去掉标记,但我怀疑/希望我可以直接在Nokogiri中以更优雅的方式完成这项工作

目前我正在使用irb计算语法:

irb>require 'rubygems'
irb>require 'nokogiri'
irb>require 'open-uri'
irb>doc = Nokogiri::XML(open('http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=KBHB'))
=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
=> <?xml version="1.0"?>
# [...]
<!-- 0.036:0 -->

irb>doc.xpath('/current_observation/weather')
=> <weather>Clear</weather>irb(main):019:0> 
irb>doc.xpath('/current_observation/wind_dir')
=> <wind_dir>North</wind_dir>
irb>doc.xpath('/current_observation/wind_mph')
=> <wind_mph>10</wind_mph>
irb>doc.xpath('/current_observation/pressure_string')
=> <pressure_string>31.10 in (1053 mb)</pressure_string>

所有返回错误。

类似的内容对我很有用:

irb(main):019:0> doc.xpath('//current_observation/weather').first.content
=> "Clear"

根据XPath,可以使用
text()
返回元素的文本节点


在您的示例中,应该是
doc.xpath('/current_observation/weather/text()')
来获取
weather的
text节点的内容。

Nokogiri的一个优点是它在编写访问器时的灵活性。您不仅限于XPath,还可以使用:

需要“rubygems”
需要“nokogiri”
需要“打开uri”
doc=Nokogiri::XML(打开)http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=KBHB'))
天气报告=%w[天气风方向风速压力字符串]。注入({}){h,n|
h[n.to_sym]=文件位置('当前天气观测'{:天气=>“阴天”,:风向=>“南方”,:风速=>“6”,:气压字符串=>“29.67英寸(1005 mb)”)

kRON是的,谢谢,就是这样。我还使用了doc.xpath(“/current\u observation/weather”).text(),但它在字符串周围返回了引号。我认为您的方法可能对我更有用。谢谢。是的,这很有效。我还尝试了doc.xpath(“/current\u observation/weather”).text()--但它会在字符串周围返回引号。您的方式可能对我更有用。谢谢,我很困惑。当我离开页面并返回时,我的评论消失了,我以为我把预览和发布混淆了。。。
irb(main):019:0> doc.xpath('//current_observation/weather').first.content
=> "Clear"
require 'rubygems'
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(open('http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=KBHB'))

weather_report = %w[weather wind_dir wind_mph pressure_string].inject({}) { |h, n|
  h[n.to_sym] = doc.at('current_observation ' << n).text
  h 
} 
weather_report # => {:weather=>"Overcast", :wind_dir=>"South", :wind_mph=>"6", :pressure_string=>"29.67 in (1005 mb)"}