我正在尝试用nokogiri解析XML文件:
require 'nokogiri'
require 'open-uri'
@doc = Nokogiri::XML(open('http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=E%20Sports&contest=no'))
@doc.xpath("//event[@league='*LOL*']")
print @doc.text
在" league"属性中起作用并打印所有包含" LOL"的事件,但是当我创建一个块时,它运行但什么也没打印:
@doc.xpath("//event[@league='*LOL*']").each do |league_element|
puts "n"+league_element.xpath('league').text
end
require 'nokogiri'
require 'open-uri'
@doc = Nokogiri::XML(open('http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=E%20Sports&contest=no'))
events = @doc.xpath("//event[@league='*LOL*']")
puts @doc.children
.children
"返回一个新的节点,其中包含节点中所有节点的所有孩子。"您可以使用children.xpath()
例如:
@doc = Nokogiri::XML(open('http://xml.pinnaclesports.com/pinnacleFeed.aspx?sportType=E%20Sports&contest=no'))
events = @doc.xpath("//event[@league='*LOL*']")
puts @doc.children.xpath('//league').text
=> LOL Cham Kor
=> LOL Cham Kor
=> ....
或
@doc.children.each do |item|
puts item.xpath('//league')
end