From: Heesob Park Date: 2009-08-06T23:00:45+09:00 Subject: Re: Basic XML to nested Array Hi, 2009/8/6 James Jhfghgfhgf : > 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? > 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 Heesob