# Ruby newbie & XML - What's the simplest way to get XML data

**URL:** <https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248>\
**Category:** Ruby\
**Created:** [November 16, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248 "2006-11-16T23:12:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 16, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/1 "2006-11-16T23:12:05Z")

</div>

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:  
  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL  
  
  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL  
  
  
NULL  
NULL  
NULL

TIA

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 16, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/2 "2006-11-16T23:26:02Z")

</div>

removed\_email\_address@domain.invalid 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 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/](http://xml-simple.rubyforge.org/)

dj

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 17, 2006, 12:16am UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/3 "2006-11-17T00:16:24Z")

</div>

removed\_email\_address@domain.invalid 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.

Try this:

* * *

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 16, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/4 "2006-11-16T23:46:17Z")

</div>

On 16.11.2006 23:06, removed\_email\_address@domain.invalid wrote:

> 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@:1(irb)  
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
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 17, 2006, 12:24am UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/5 "2006-11-17T00:24:27Z")

</div>

Paul L. wrote:

> * * *
> 
> * * *

The Zen of XML 🙂

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 17, 2006, 12:26am UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/6 "2006-11-17T00:26:29Z")

</div>

Paul L. wrote:

> * * *
> 
> * * *

Sorry. My Usenet reader went crazy.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 17, 2006, 12:20am UTC](https://www.ruby-forum.com/t/ruby-newbie-xml-whats-the-simplest-way-to-get-xml-data/76248/7 "2006-11-17T00:20:25Z")

</div>

removed\_email\_address@domain.invalid 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.

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

traverse\_hash(root)

* * *

Output:

environment  
authentication  
adminGroup  
dataType = NULL  
description = NULL  
value = NULL  
bindUserID  
dataType = NULL  
description = NULL  
value = NULL  
bindUserPassword  
dataType = NULL  
description = NULL  
value = NULL  
ldapServer  
dataType = NULL  
description = NULL  
value = NULL  
database  
databaseHost  
dataType = NULL  
description = NULL  
value = NULL  
databasePassword  
dataType = NULL  
description = NULL  
value = NULL  
databasePort  
dataType = NULL  
description = NULL  
value = NULL  
databaseSchema  
dataType = NULL  
description = NULL  
value = NULL  
databaseUser  
dataType = NULL  
description = NULL  
value = NULL
