[QUIZ] GEDCOM Parser (#6)

C

Cedric Foll

Hi,

Here is my solution.

I hope it would be the shortest one.

I convert the file into a Hash and then, use XML-Simple in order to
convert the Hash into an XML File.

I've supposed the deep level won't be greater than 2 (in the example
given it's the case ...).
A general solution would be a little more tricky.
I've done something with 'eval' but it's very dirty. So I didn't gave
the general solution.

----------------------------
#!/usr/bin/ruby
require 'xmlsimple'
res = Hash.new
tag_niv = []
while line = gets
if line =~ /^0 @([A-Z0-9]+)@ (\w+)$/
id,type = $1,$2
res[type] = [] if !res[type]
res[type] << Hash.new
res[type][-1]['id'] = id
tag_niv[0] = 0
elsif line =~ /^(\d+)\W+(\w+)\W+(.*)$/ and id
num,tag,data = $1.to_i,$2,$3
tag_niv[num] = $2
if num == 1
res[type][-1][tag] = [data]
elsif num == 2
res[type][-1][tag_niv[1]] << Hash.new if
res[type][-1][tag_niv[1]].length == 1
res[type][-1][tag_niv[1]][-1][tag] = [data]
end
end
end
puts XmlSimple.xml_out(res)
------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top