Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7824331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:33:47+00:00 2026-06-02T08:33:47+00:00

Here’s what I’m trying to accomplish. I need to have a single CSV with

  • 0

Here’s what I’m trying to accomplish. I need to have a single CSV with headers and several rows. I’m iterating through the headers and storing then and then associating the row data to the header. I need to be able to iterate through each of the rows in the CSV to use for constructing an XML’s data. The constructed XML is then dumped as a .xml file and the program starts on the next row in the CSV. Each row has a column that provides the name of the XML file.

Here’s what I’ve got so far.

Read in the data from the CSV file. Collect the header and row data.

def get_rows
        raw_data = CSV.read('test.csv', {:skip_blanks => false, :headers => true})
        data = []
        raw_data.each { |row| data << row}
        return build_header(data, raw_data)
    end

take the header and row data and marry them up.

def build_header(data, raw_data)
    (0..(data.length - 1)).each do |ri|
        h = {}
        raw_data.headers.each_with_index do |v, i|
            h[v] = data[ri].fields[i]
        end
        return build_ostruct(h)
    end
end

take the hash h and make an ostruct of it.

def build_ostruct(h)
    x = OpenStruct.new(h)
    uniq = x.tc_name
    y = uniq_name.to_s + ".xml"
    #marshal dump for debugging
    x.marshal_dump.each{ |k,v| puts "#{k} => #{v}" }
    return xml_builder(x, y)

end

Below this I’m taking the new ostruct “x” and calling the column headers from the CSV to #populate the XML nodes

For example: x.column1, x.column2, x.column3

Now the part I’m getting hung up on is getting the ostruct to receive the new row of data per iteration run. The objective is to have the ostruct populate with each row from the CSV. Currently the hash is displaying the proper data set and my XML is populating as expected but only with the first row of data. How do I get this to iterate through all the rows and populate the ostruct with the data per iteration so I can create a bulk set of XML’s?

Thanks in advance for any and all help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:33:50+00:00Added an answer on June 2, 2026 at 8:33 am

    Something like this should work:

    require 'csv'
    require 'nokogiri'
    
    CSV.foreach('test.csv', :headers => true) do |row|
        builder = Nokogiri::XML::Builder.new do |xml|
            xml.root do |root|
                row.each do |k, v|
                    root.send k, v
                end
            end
        end
        File.open("#{row['tc_name']}.xml", 'w'){|f| f << builder.to_xml}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the problem that I am trying to solve. I have two folders
Here is my code (Say we have a single button on the page that
Here is what I am trying to achieve in PHP: I have this string:
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example: I write html code inside of textarea, then I swap
Here is the css: #content ul { font-size: 12px; } I am trying this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.