1
我想解析一堆XML文件。我正在使用Nokogiri,Ruby和XPath。但没有得到任何结果。我做错了什么,对于一些提示或一些代码示例会很有帮助。的XML文件的从红宝石nokogiri读取XML的URL
实施例: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
([email protected] - 1).each do |index|
puts "LOCAL: #{@locality[index]}"
end
end
编辑: 问题是在xpath表达式。正确的表达是: // educationprovider // vcard // adr // locality
你不能'doc = Nokogiri :: HTML(open('link'))',但你可以'doc = Nokogiri: :HTML(开放( 'http://www.example.com'))'。对于将来的问题,无论何时您希望获得有关XML或HTML问题的帮助,我们都需要能够访问数据样本或实时数据。试图在没有它的情况下回答这个问题几乎是不可能的。 – 2012-02-09 23:02:55