From: Mark Thomas Date: 2009-07-21T21:25:06+09:00 Subject: Re: XML output (urgent) On Jul 20, 10:13 pm, Hv Mai wrote: > Hi all, > > >> t = {'type' => 'abc', 'numbers' => {'number' => [333], 'src' => 'glasses'}} > > => {"type"=>"abc", "numbers"=>{"number"=>[333], "src"=>"glasses"}}>> puts XmlSimple.xml_out({"policyDetails" => t},{"KeepRoot" => true}) > > >   >     333 >   > > > Can you please give me a hint that how to output the xml likes below ... > > >> puts XmlSimple.xml_out({"policyDetails" => t},{"KeepRoot" => true}) > > >   333 > The trick with XmlSimple is that it can round-trip. In other words, you can perform xml_in() on the output you want and you'll see exactly what you need for xml_out() to produce it. so, you can do this: require 'xmlsimple' data = ' 333 ' xml = XmlSimple::xml_in(data, {"KeepRoot" => true}) p xml and you'll get {"policyDetails"=>[{"type"=>"abc", "numbers"=>[{"src"=>"glasses", "content"=>"333"}]}]} and therefore, if you perform xml_out on the above like so: data = {"policyDetails"=>[{"type"=>"abc", "numbers"=> [{"src"=>"glasses", "content"=>"333"}]}]} puts XmlSimple::xml_out(data, {"KeepRoot" => true}) you get: 333 ...exactly what you want. -- Mark. P.S. I hadn't used XmlSimple before, but the docs on the xml_out method say "If the resulting XML is parsed using xml_in, it will return a data structure equivalent to the original." so I realized the above trick would work.