Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.
I'm thinking of some sort of data structure like so:
Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.
I think most people would think a specific question something to be
admired, not something to be apologised for!
Are you aware of the Ruby library xml-simple, which is a translation of
the Perl XML::Simple
Now you just need a XML stream parser (REXML has one) and fill the Hash while you go.
But my question to this is, why do you want it in a Hash of Hashes when there is REXML with an equally easy accessible DOM and which knows more about the data than nested hashes (order for example)? Also, you can access nested elements with XPath which is a powerful query language. Granted, accesses are likely less efficient than with the nested Hash approach but do you need the speed?
Kind regards
robert
···
On 16.11.2006 23:06, darenbell@gmail.com wrote:
Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.
I'm thinking of some sort of data structure like so:
Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.
Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.
Try this:
···
---------------------------------
#!/usr/bin/ruby -w
data = File.read("data.xml")
root = {}
hash = root
stack =
data.each do |line|
line.strip!
if line =~ %r{^<\w+>$}
tag = line.sub(%r{<(.*?)>},"\\1")
stack.push(hash)
hash[tag] ||= {}
hash = hash[tag]
elsif line =~ %r{^</\w+>$}
hash = stack.pop
elsif line =~ %r{<(\w+)>.*?</\1>}
key,value = line.scan(%r{<(\w+)>(.*?)<}).flatten
hash[key] = value
else
puts "Error: undefined case: #{line}"
end
end
def traverse_hash(hash,tab = 0)
hash.keys.sort.each do |key|
print "#{" " * tab}"
if(hash[key].class == Hash)
puts "#{key}"
traverse_hash(hash[key],tab+1)
else
puts "#{key} = #{hash[key]}"
end
end
end
Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.
Try this:
-----------------------------------
-----------------------------------
The Zen of XML
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Sorry for having such a specific question, but is there a simple way to
take the XML document below and put it into some sort of organized hash
value without first knowing anything about it?
This is an easy job using something like XML::Simple in Perl, but for
the life of me, I can't figure out an easy way to do this in Ruby.