Skip to content

Instantly share code, notes, and snippets.

@mps
Created June 20, 2013 22:02
Show Gist options
  • Save mps/5827103 to your computer and use it in GitHub Desktop.
Save mps/5827103 to your computer and use it in GitHub Desktop.
Open an XML file and download images from its nodes...
require 'rubygems'
require 'nokogiri'
require 'open-uri'
XML_FILE_LOCATION = 'path/to/my/xml/file.xml'
XML_XPATH = '//ANode'
XML_ATTR_FOR_IMAGE_NAME = 'imagename'
XML_ATTR_FOR_IMAGE_URL = 'imageurl'
file = File.open(XML_FILE_LOCATION, "rb")
xml_string = file.read
xml = Nokogiri::XML( _xml_string )
img_srcs = xml.xpath(XML_XPATH)
img_srcs.each do |img|
image_name = img.attr(XML_ATTR_FOR_IMAGE_NAME)
image_url = img.attr(XML_ATTR_FOR_IMAGE_URL)
open(image_url) {|f|
File.open(image_name,"wb") do |file|
file.puts f.read
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment