Ruby newbie & XML - What's the simplest way to get XML data into a hash?

D

darenbell

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:

environment=>authentication=>adminGroup=>description=>'NULL'
environment=>authentication=>adminGroup=>dataType=>'NULL'
environment=>authentication=>adminGroup=>value=>'NULL'
etc...
environment=>database=>databaseHost=>description=>'NULL'
environment=>database=>databaseHost=>dataType=>'NULL'
etc...


XML document:
<environment>
<authentication>
<adminGroup>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</adminGroup>
<bindUserID>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</bindUserID>
<bindUserPassword>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</bindUserPassword>
<ldapServer>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</ldapServer>
</authentication>
<database>
<databaseHost>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</databaseHost>
<databaseSchema>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</databaseSchema>
<databasePort>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</databasePort>
<databaseUser>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</databaseUser>
<databasePassword>
<description>NULL</description>
<dataType>NULL</dataType>
<value>NULL</value>
</databasePassword>
</database>
</environment>


TIA
 
D

David Roberts

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

Details at: http://xml-simple.rubyforge.org/

dj
 
R

Robert Klemme

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:

environment=>authentication=>adminGroup=>description=>'NULL'
environment=>authentication=>adminGroup=>dataType=>'NULL'
environment=>authentication=>adminGroup=>value=>'NULL'
etc...
environment=>database=>databaseHost=>description=>'NULL'
environment=>database=>databaseHost=>dataType=>'NULL'
etc...

You can easily create nested Hashes with the typical idiom:

irb(main):001:0> miss = lambda {|h,k| h[k] = Hash.new(&miss)}
=> #<Proc:0x003cbaa8@(irb):1>
irb(main):002:0> root = Hash.new(&miss)
=> {}
irb(main):003:0> root[:foo][:bar][:baz] = 10
=> 10
irb(main):004:0> root
=> {:foo=>{:bar=>{:baz=>10}}}

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top