Created
June 20, 2013 22:02
-
-
Save mps/5827103 to your computer and use it in GitHub Desktop.
Open an XML file and download images from its nodes...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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