Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
CSV nesting
Max Russell
4/21/2008 2:32:00 PM
I have a snippet of code that looks like this:
next if row.nil?
for i in 0...row.length
next if row[i].nil?
f.puts " <#{fields[i]}>#{row[i]}</#{fields[i]}>"
end
this is part of a small script that parses a CSV document and then
creates an XML output file.
However, I would like to add support for nesting elements in the output,
so that if the current column/value is surname or forename I can write
my output in a nested fashion:
<PERSON>
<SURNAME>Brown</SURNAME>
<FORENAME>Bobby</FORENAME>
</PERSON>
The problem I'm having is with the identification of the current element
as either SURNAME or FORENAME
I've tried using:
if #{fields[i]} == "<ORGANISATION_PERSON_SURNAME>"
(where fields is CSV.shift) and I've also tried
if row[i].data == 'ORGANISATION_PERSON_SURNAME'
What is the best way of acessing this please?
I've attached my original script for perusal.
Attachments:
http://www.ruby-...
attachment/1765/...
--
Posted via
http://www.ruby-...
.
3 Answers
Jeremy Hinegardner
4/21/2008 3:03:00 PM
0
On Mon, Apr 21, 2008 at 11:32:01PM +0900, Max Russell wrote:
> I have a snippet of code that looks like this:
>
> next if row.nil?
> for i in 0...row.length
> next if row[i].nil?
> f.puts " <#{fields[i]}>#{row[i]}</#{fields[i]}>"
> end
>
> this is part of a small script that parses a CSV document and then
> creates an XML output file.
>
> However, I would like to add support for nesting elements in the output,
> so that if the current column/value is surname or forename I can write
> my output in a nested fashion:
>
> <PERSON>
> <SURNAME>Brown</SURNAME>
> <FORENAME>Bobby</FORENAME>
> </PERSON>
>
> The problem I'm having is with the identification of the current element
> as either SURNAME or FORENAME
>
> I've tried using:
>
> if #{fields[i]} == "<ORGANISATION_PERSON_SURNAME>"
>
> (where fields is CSV.shift) and I've also tried
>
> if row[i].data == 'ORGANISATION_PERSON_SURNAME'
>
> What is the best way of acessing this please?
I would use FasterCSV and loop over each row, and have some controlling
information that shows which row fields should be grouped into a higher tag.
> I've attached my original script for perusal.
>
> Attachments:
>
http://www.ruby-forum.com/attachment/1765/...
Completely off the cuff, untested, but this is the approach I'm thinking of.
http://p.ramaz...
enjoy,
-jeremy
--
========================================================================
Jeremy Hinegardner jeremy@hinegardner.org
Max Russell
4/22/2008 1:05:00 PM
0
Jeremy Hinegardner wrote:
> Completely off the cuff, untested, but this is the approach I'm thinking
> of.
>
>
http://p.ramaz...
>
> enjoy,
>
> -jeremy
Thanks, although after looking at the FasterCSV docs. I'm thinking of
working with an array or arrays instead of a Hash.
--
Posted via
http://www.ruby-...
.
Max Russell
4/23/2008 11:00:00 AM
0
Max Russell wrote:
> Jeremy Hinegardner wrote:
>
>> Completely off the cuff, untested, but this is the approach I'm thinking
>> of.
>>
>>
http://p.ramaz...
>>
>> enjoy,
>>
>> -jeremy
>
> Thanks, although after looking at the FasterCSV docs. I'm thinking of
> working with an array or arrays instead of a Hash.
I've attached what I'm now using. I'm finding however that, due to the
condition execution, my current code spits out all the other attributes
before the two conditions:
for i in 1..rows.length
next if rows[i].nil?
if not rows[surname] or rows[forename]
puts rows[i]#"Current attribute: #{rows[i]}"
end
if rows[surname]
puts "<ORGANISATIONAL_PERSON>"
#puts "row #{i}"
puts " <SURNAME>"
puts " #{rows[i][surname]}"
puts " </SURNAME>"
end
if rows[forename]
#puts "row #{i}"
puts " <FORENAME>"
puts " #{rows[i][forename]}"
puts " </FORENAME>"
end
end
The problem being that an if, elsif else construction doesn't seem to
work...
Attachments:
http://www.ruby-...
attachment/1769/fstcsv_xml_...
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
CSV nesting
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password