使用ruby nokogiri从url读取xml



我正在尝试解析一堆XML文件。我使用的是Nokogiri、Ruby和XPath。但是没有得到任何结果。我做错了什么,如果有一些提示或代码示例,那将非常有用。

XML文件示例:XML链接

这是我的RUBY脚本:

require 'rubygems'
require 'nokogiri'
require 'open-uri'
# parse the HTML document with all the links to the XML files.
doc = Nokogiri::HTML(open('link'))
# URLS - array
@urls = Array.new 
#Get all XML-urls and save them in urls-array
doc.xpath('//a/@href').each do |links|
  @urls << links.content
end
#LOCALITY array
@locality = Array.new
# loop all the url of the XML files
@urls.each do |url|
  doc = Nokogiri::HTML(open(url))
  # grab the content I want
  doc.xpath('//educationprovider//vcard//adr/locality').each do |locality_node| 
   # store it in locality array
    @locality << locality_node.content
  end
  # loop the the locality array and print it out
  (0..@locality.length - 1).each do |index|
    puts "LOCAL: #{@locality[index]}"
  end  
end

编辑:问题出现在xpath表达式中。正确的表述是://教育提供者//vcard//adr//locality

问题出在xpath表达式中。正确的表达式是://educationprovider//vcard//adr//locality

最新更新