开发者

Array to XML -- Rails

开发者 https://www.devze.com 2022-12-10 07:28 出处:网络
I have a multi-dimensional array that I\'d like to use for building an xml output. The array is storing a csv import. Where people[0][...] are the column names that will become the xml tags, and the

I have a multi-dimensional array that I'd like to use for building an xml output.

The array is storing a csv import. Where people[0][...] are the column names that will become the xml tags, and the people[...>0][...] are the values.

For instance, array contains:
people[0][0] => first-name
people[0][1] => last-name
people[1][0] => Bob
people[1][1] => Dylan
people[2][0开发者_C百科] => Sam
people[2][1] => Shepard

XML needs to be:
<person>
  <first-name>Bob</first-name>
  <last-name>Dylan</last-name>
</person>
<person>
  <first-name>Sam</first-name>
  <last-name>Shepard</last-name>
</person>

Any help is appreciated.


I suggest using FasterCSV to import your data and to convert it into an array of hashes. That way to_xml should give you what you want:

people = []
FasterCSV.foreach("yourfile.csv", :headers => true) do |row|
 people << row.to_hash
end
people.to_xml


There are two main ways I can think of achieving this, one using an XML serializer; the second by pushing out the raw string.

Here's an example of the second:

xml = ''
1.upto(people.size-1) do |row_idx|
  xml << "<person>\n"
  people[0].each_with_index do |column, col_idx|
    xml << "  <#{column}>#{people[row_idx][col_idx]}</#{column}>\n"
  end
  xml << "</person>\n"
end

Another way:

hash = {}
hash['person'] = []
1.upto(people.size-1) do |row_idx|
  row = {}
  people[0].each_with_index do |column, col_idx|
    row[column]=people[row_idx][col_idx]
  end
  hash['person'] << row
end
hash.to_xml

Leaving this answer here in case someone needs to convert an array like this that didn't come from a CSV file (or if they can't use FasterCSV).


Using Hash.to_xml is a good idea, due to its support in the core rails. It's probably the simplest way to export Hash-like data to simple XML. In most, simple cases - more complex cases requires more complex tools.


Thanks to everyone that posted. Below is the solution that seems to work best for my needs. Hopefully others may find this useful.

This solution grabs a remote url csv file, stores it in a multi-dimensional array, then exports it as xml:

require 'rio'
require 'fastercsv'

url = 'http://remote-url.com/file.csv'
people = FasterCSV.parse(rio(url).read)

xml = ''
1.upto(people.size-1) do |row_idx|
  xml << "  <record>\n"
  people[0].each_with_index do |column, col_idx|
    xml << "    <#{column.parameterize}>#{people[row_idx][col_idx]}</#{column.parameterize}>\n"
  end
  xml << "  </record>\n"
end

There are better solutions out there, using hash.to_xml would have been great except I needed to change the csv index line to parameterize to use as a xml tag, but this code works so I'm happy.

0

精彩评论

暂无评论...
验证码 换一张
取 消