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?

4
  • please rephrase your question, it's hard to say what you want. Commented May 17, 2011 at 14:15
  • There's a problem when you need to loop? Can you be a bit more specific about that? Commented May 17, 2011 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. <root><subroot id=(Test)>....fields... </subroot><subroot id=(Test)>.....fields....</subroot></root> Sorry, but its kind of hard to write it down clearly. Commented May 18, 2011 at 19:03
  • tried to use stackoverflow.com/questions/5193587/… 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.. Commented May 18, 2011 at 20:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.