user592638
user592638

Reputation:

Reading xml from urls with ruby nokogiri

I am trying to parse a bunch of XML files. I am using Nokogiri, Ruby and XPath. But dont get any result. What are I am doing wrong, would be greatful for some tips or some code samples.

Example of the XML-file: xml-link

HERE IS MY RUBY SCRIPT:

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

EDIT: The problem was in xpath expression. The right expression was: //educationprovider//vcard//adr//locality

Upvotes: 1

Views: 2981

Answers (1)

user592638
user592638

Reputation:

The problem was in xpath expression. The right expression was: //educationprovider//vcard//adr//locality

Upvotes: 1

Related Questions