krunal shah
krunal shah

Reputation: 16339

Convert xml to csv in rails

Which plugins or gems are available to convert xml to csv file ?

Upvotes: 2

Views: 1628

Answers (2)

Pratap
Pratap

Reputation: 337

Using Nokogiri in Rails.

 xml_file = "datafile.xml"
 doc = Nokogiri::XML.parse(xml_file)
 output = "data.csv"

 sv_string = CSV.generate do |csv|
  # header row
     csv << ['header_array']

  # data rows
    @object_array.map do |object|
      csv << [object['NODE_NAME'].text]
    end
  end

 #send response  

   send_data csv_string,
          :type => 'text/csv; charset=iso-8859-1; header=present',
          :disposition => "attachment; filename=users.csv" 

Upvotes: 0

jdl
jdl

Reputation: 17790

Since the two formats generally have nothing to do with each other, I would suggest parsing the XML with Nokogiri and then building up your CSV with FasterCSV. You need to provide the logic in between to decide which parts go where in your CSV file.

Upvotes: 7

Related Questions