Basic XML to nested Array

Discussion in 'Ruby' started by James Jhfghgfhgf, Aug 6, 2009.

  1. 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:
    <vector>
    <point>
    <x>1018.550659</x>
    <y>558.138367</y>
    <z>102.505821</z>
    </point>
    <point>
    <x>1013.557495</x>
    <y>560.874878</y>
    <z>107.041840</z>
    </point>
    <point>
    <x>1008.563049</x>
    <y>563.611267</y>
    <z>111.577576</z>
    </point>
    </vector>
    <vector>
    <point>
    <x>1030.809204</x>
    <y>551.722961</y>
    <z>86.581741</z>
    </point>
    <point>
    <x>1027.500854</x>
    <y>553.190613</y>
    <z>95.061058</z>
    </point>
    </vector>

    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!
    --
    Posted via http://www.ruby-forum.com/.
     
    James Jhfghgfhgf, Aug 6, 2009
    #1
    1. Advertising

  2. James Jhfghgfhgf

    Heesob Park Guest

    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:
    > <vector>
    > =C2=A0<point>
    > =C2=A0 =C2=A0 =C2=A0<x>1018.550659</x>
    > =C2=A0 =C2=A0 =C2=A0<y>558.138367</y>
    > =C2=A0 =C2=A0 =C2=A0<z>102.505821</z>
    > =C2=A0</point>
    > =C2=A0<point>
    > =C2=A0 =C2=A0 =C2=A0<x>1013.557495</x>
    > =C2=A0 =C2=A0 =C2=A0<y>560.874878</y>
    > =C2=A0 =C2=A0 =C2=A0<z>107.041840</z>
    > =C2=A0</point>
    > =C2=A0<point>
    > =C2=A0 =C2=A0 =C2=A0<x>1008.563049</x>
    > =C2=A0 =C2=A0 =C2=A0<y>563.611267</y>
    > =C2=A0 =C2=A0 =C2=A0<z>111.577576</z>
    > =C2=A0</point>
    > </vector>
    > <vector>
    > =C2=A0<point>
    > =C2=A0 =C2=A0 =C2=A0<x>1030.809204</x>
    > =C2=A0 =C2=A0 =C2=A0<y>551.722961</y>
    > =C2=A0 =C2=A0 =C2=A0<z>86.581741</z>
    > =C2=A0</point>
    > =C2=A0<point>
    > =C2=A0 =C2=A0 =C2=A0<x>1027.500854</x>
    > =C2=A0 =C2=A0 =C2=A0<y>553.190613</y>
    > =C2=A0 =C2=A0 =C2=A0<z>95.061058</z>
    > =C2=A0</point>
    > </vector>
    >
    > and I want to make it this:
    > [[[1018.550659],[558.138367],[102.505821]],[[1013.557495],[560.874878],[1=

    07.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 =3D REXML::Document.new("<root>"+File.read("a.xml")+"</root>")
    res =3D 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
     
    Heesob Park, Aug 6, 2009
    #2
    1. Advertising

  3. 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("<root>"+File.read("a.xml")+"</root>")
    > res = doc.elements.to_a("/*/*").map{|e1|
    > e1.elements.to_a.map{|e2|e2.elements.to_a.map{|e3|e3.text}}
    > }
    > p res

    --
    Posted via http://www.ruby-forum.com/.
     
    James Jhfghgfhgf, Aug 6, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,288
    Russ Perry Jr
    Aug 20, 2004
  2. Engineer
    Replies:
    6
    Views:
    676
    Jeremy Bowers
    May 1, 2005
  3. Replies:
    0
    Views:
    474
  4. Ultrus
    Replies:
    3
    Views:
    412
    Stefan Behnel
    Jul 9, 2007
  5. Originlabs
    Replies:
    0
    Views:
    468
    Originlabs
    Apr 23, 2008
Loading...

Share This Page