# XML output (urgent)

**URL:** <https://www.ruby-forum.com/t/xml-output-urgent/172410>\
**Category:** Ruby\
**Created:** [July 21, 2009, 4:13am UTC](https://www.ruby-forum.com/t/xml-output-urgent/172410 "2009-07-21T04:13:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hvmai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hvmai](https://www.ruby-forum.com/u/hvmai)\
**Post date:** [July 21, 2009, 4:13am UTC](https://www.ruby-forum.com/t/xml-output-urgent/172410/1 "2009-07-21T04:13:25Z")

</div>

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

Many thanks.

---

<div class="post-metadata">

**Author:** ![hvmai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hvmai](https://www.ruby-forum.com/u/hvmai)\
**Post date:** [July 21, 2009, 8:24am UTC](https://www.ruby-forum.com/t/xml-output-urgent/172410/2 "2009-07-21T08:24:57Z")

</div>

Iâ€™m not familiar with `XmlSimple`, but may I suggest Nokogiri?

---

<div class="post-metadata">

**Author:** ![hvmai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hvmai](https://www.ruby-forum.com/u/hvmai)\
**Post date:** [July 21, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/xml-output-urgent/172410/3 "2009-07-21T14:25:35Z")

</div>

On Jul 20, 10:13 pm, Hv Mai [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.
