0

I have a hash that i wanted to parse to XML using SimpleXML but there is a trick I dont know how to handle:

My hash looks like:

require 'xmlsimple'
test = { "subroot" => {
  field1 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
  field2 => {'var1' = ['xyz'], 'var2' = ['yyyy']},
  field3 => {'var1' = ['xyz'], 'var2' = ['yyyy']}, 
  'id' = 'xxxxxx'} }

I parse it to XML using:

XmlSimple.xml_out(teste, 'RootName' => 'root') 

resulting in:

<cenario>
  <subroot id="xxxxxx">
     <field1>
       <var1>xyz</var1>
       <var2>yyyy</var2>
     </field1>
     <field2>
       <var1>xyz</var1>
       <var2>yyyy</var2>
     </field2>
     <field3>
       <var1>xyz</var1>
       <var2>yyyy</var2>
     </field3>
   </subroot>
</cenario>

That's OK and works like a charm but there is problem when I need to loop. Im doing a automated test that loops on each test and save the test data on a Hash and by the end of the tests i need to save it on a xml and i need to get something like this:

<cenario>
      <subroot id="xxxxxx">
         <field1>
           <var1>xyz</var1>
           <var2>yyyy</var2>
         </field1>
         <field2>
           <var1>xyz</var1>
           <var2>yyyy</var2>
         </field2>
         <field3>
           <var1>xyz</var1>
           <var2>yyyy</var2>
         </field3>
       </subroot>
      .
      .
      .
      <subroot id="xxxx10x">
         <field1>
           <var1>xyz</var1>
           <var2>yyyy</var2>
         </field1>
         <field2>
           <var1>xyz</var1>
           <var2>yyyy</var2>
         </field2>
         <field3>
           <var1>xyz</var1>
           <var2>yyyy</var2>
         </field3>
       </subroot>
    </cenario>

merging each hash on each test will not do the trick cz it prevents the last hash (subroot) Did i made myself clear? Can't simpleXML do the trick or do I need to look for another solution?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Leonardo Galani
  • 184
  • 5
  • 22
  • please rephrase your question, it's hard to say what you want. – tokland May 17 '11 at 14:15
  • There's a problem when you need to loop? Can you be a bit more specific about that? – Mark Thomas May 17 '11 at 14:18
  • Sorry, bad english. Im doing a automated test script thats need to save each test data for further check. For accomplish that, my app save each test loop( Junit... @test... @parametrization..got it?) on a hash and by the end of the tests, i need to save it as a XML. Problem is that that i cant repeat the "subroot" name on the hash (duhh) but my xml gotta have same pattern. ....fields... .....fields.... Sorry, but its kind of hard to write it down clearly. – Leonardo Galani May 18 '11 at 19:03
  • tried to use http://stackoverflow.com/questions/5193587/converting-hash-to-xml-using-xmlsimple-in-ruby solution but it didnt worked out... changed subroot text for a String with test id... hash populated right but when i convert it.. it deletes all the entries except the last one.. – Leonardo Galani May 18 '11 at 20:32

1 Answers1

0

Thks for the help...

I figured out that i dont need Hash keys if i put a "instance" of the hash inside a array... I can use the same Hash name, each array slot saves a instance of my hash than i can call simpleXML or to_xml

No i just need to figure out how to remove the objects tags that makes the xml bigger.

Leonardo Galani
  • 184
  • 5
  • 22