Skip to content

Instantly share code, notes, and snippets.

@jayfallon
Created February 22, 2010 18:30
Show Gist options
  • Save jayfallon/311324 to your computer and use it in GitHub Desktop.
Save jayfallon/311324 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'csv'
print "CSV file to read: "
input_file = gets.chomp
print "File to write XML to: "
output_file = gets.chomp
csv = CSV::parse(File.open(input_file) {|f| f.read} )
fields = csv.shift
puts "Writing XML..."
File.open(output_file, 'w') do |f|
f.puts '<?xml version="1.0" encoding="UTF-8"?>'
f.puts '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
csv.each do |record|
f.puts ' <url>'
for i in 0..(fields.length - 1)
f.puts " <loc>#{record[i]}</loc>"
end
f.puts ' <changefreq>weekly</changefreq>'
f.puts ' <priority>0.8</priority>'
f.puts ' </url>'
end
f.puts '</urlset>'
end # End file block - close file
puts "Contents of #{input_file} written as XML to #{output_file}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment