Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Reading an xml file from a URL and saving it locally (See related posts)

// description of your code here
This code reads an xml file, modifys an element and saves the file locally.
require 'net/http'
require 'rexml/document'

url = 'http://www.example.com/journal080907.xml'

element_name = ARGV[0] # basic_category
xpath = ARGV[1] # eg.'entries/entry'
file = ARGV[2] # eg. 'journal080907.xml'
element_value = ARGV[3] # 'ruby'

file = File.new(file,'w')
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body

# extract event information
doc = REXML::Document.new(xml_data)
docx = doc

node = docx.elements[xpath]
element = node.elements[element_name]
puts element.text
element.text = 'ruby'
file.puts docx

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts