my answer for quiz #3. I first used rexml, but I hated it. Now I'm using nokigiri... master
authorDavid Blacka <david@blacka.com>
Thu, 27 Oct 2011 01:03:50 +0000 (21:03 -0400)
committerDavid Blacka <david@blacka.com>
Thu, 27 Oct 2011 01:03:50 +0000 (21:03 -0400)
gedcom_to_xml.rb [new file with mode: 0755]

diff --git a/gedcom_to_xml.rb b/gedcom_to_xml.rb
new file mode 100755 (executable)
index 0000000..f830238
--- /dev/null
@@ -0,0 +1,35 @@
+#! /usr/bin/env ruby
+
+require 'nokogiri'
+include Nokogiri::XML
+
+doc    = Document.new
+gedcom = Node.new "gedcom", doc
+
+doc << gedcom
+level_element = []
+
+IO.foreach(ARGV[0]) do |line|
+  next if line =~ /^\s*$/
+
+  # print "line:", line
+  level, tag_or_id, data = line.split(/\s+/, 3)
+  level = Integer(level)
+  parent = level == 0 ? gedcom : level_element[level - 1]
+
+  if tag_or_id =~/^@(.*)@$/
+    el = Node.new data.downcase.chomp, doc
+    el['id'] = $1
+  else
+    el = Node.new tag_or_id.downcase, doc
+    el.content = data.chomp
+  end
+
+  # puts "for level #{level}, adding #{el.name} to #{parent.name}"
+  parent << el
+
+  level_element[level] = el
+end
+
+puts doc.to_xml()
+