# Basic XML to nested Array

**URL:** <https://www.ruby-forum.com/t/basic-xml-to-nested-array/173306>\
**Category:** Ruby\
**Created:** [August 6, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/basic-xml-to-nested-array/173306 "2009-08-06T14:22:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mode](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@mode](https://www.ruby-forum.com/u/mode)\
**Post date:** [August 6, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/basic-xml-to-nested-array/173306/1 "2009-08-06T14:22:45Z")

</div>

Hey,

I’ve been having some trouble getting to grips with the various XML  
libraries, I just want to put together a ruby script that will convert a  
basic XML file into a nested array then just output that array to the  
console.

The XML looks like this:  
  
  
1018.550659  
558.138367  
102.505821  
  
  
1013.557495  
560.874878  
107.041840  
  
  
1008.563049  
563.611267  
111.577576  
  
  
  
  
1030.809204  
551.722961  
86.581741  
  
  
1027.500854  
553.190613  
95.061058

and I want to make it this:  
[[[1018.550659],[558.138367],[102.505821]],[[1013.557495],[560.874878],[107.041840]],[[1008.563049],[563.611267],[111.577576]]],[[[1030.809204],[551.722961],[86.581741]],[[1027.500854],[553.190613],[95.061058]]]

Anyone know the best way to go about this?

Thanks very much!

---

<div class="post-metadata">

**Author:** ![mode](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@mode](https://www.ruby-forum.com/u/mode)\
**Post date:** [August 6, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/basic-xml-to-nested-array/173306/2 "2009-08-06T16:01:32Z")

</div>

Hi,

2009/8/6 James J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â Â 1018.550659  
> Â Â Â 563.611267  
> Â Â Â 1027.500854  
> Â Â Â 553.190613  
> Â Â Â 95.061058  
> Â
> 
> and I want to make it this:  
> [[[1018.550659],[558.138367],[102.505821]],[[1013.557495],[560.874878],[107.041840]],[[1008.563049],[563.611267],[111.577576]]],[[[1030.809204],[551.722961],[86.581741]],[[1027.500854],[553.190613],[95.061058]]]
> 
> Anyone know the best way to go about this?

Your xml file is missing the root element.  
If your xml file is ‘a.xml’, you can do something like this:

require ‘rexml/document’  
doc = REXML::Document.new(“”+File.read(“a.xml”)+“”)  
res = doc.elements.to\_a(“/_/_”).map{|e1|  
e1.elements.to\_a.map{|e2|e2.elements.to\_a.map{|e3|e3.text}}  
}  
p res

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![mode](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@mode](https://www.ruby-forum.com/u/mode)\
**Post date:** [August 6, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/basic-xml-to-nested-array/173306/3 "2009-08-06T17:43:17Z")

</div>

Yeah the program outputting this stuff doesn’t add a root element so  
great tip there, it worked perfectly thanks Heesob!

> require ‘rexml/document’  
> doc = REXML::Document.new(""+File.read(“a.xml”)+"")  
> res = doc.elements.to\_a("/_/_").map{|e1|  
> e1.elements.to\_a.map{|e2|e2.elements.to\_a.map{|e3|e3.text}}  
> }  
> p res
