2012-02-09 86 views
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

+0

你不能'doc = Nokogiri :: HTML(open('link'))',但你可以'doc = Nokogiri: :HTML(開放( 'http://www.example.com'))'。對於將來的問題,無論何時您希望獲得有關XML或HTML問題的幫助,我們都需要能夠訪問數據樣本或實時數據。試圖在沒有它的情況下回答這個問題幾乎是不可能的。 – 2012-02-09 23:02:55

回答

1

問題出在xpath表達式中。 正確的表達是:// educationprovider // vcard // adr // locality