Created
May 15, 2014 15:35
-
-
Save billspat/178f94d563dc1d819931 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# very simple csv to xml convert, targeted for ruby 2.0 + | |
# depends on activesupport 4 gem which add #to_xml method to hash and array | |
require 'csv' | |
require "active_support/core_ext" | |
if ARGV.length < 1 || ARGV[0] == "-h" | |
puts "usage : ruby csv2xml.rb myfile.csv > myfile.xml" | |
exit | |
end | |
infile = ARGV[0] | |
csv = CSV.new(File.new(infile), :headers => true, :header_converters => :symbol, :converters => [:all]) | |
row_hash = csv.to_a.map{|row| row.to_hash } | |
file_name_for_root_element= File.basename(infile, File.extname(infile)) | |
puts row_hash.to_xml(:root => file_name_for_root_element) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment