Forum: Ruby How to eliminate hash tag when to_xml is used.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Song C. (Guest)
on 2009-04-29 04:35
(Received via mailing list)
# help

Hi, everyone,


I have a hash obj and when converted to xml using to_xml the
<hash></hash> tag appears like the following

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <step1>
...
</hash>

I want to get rid of that <hash> so that it is just
<?xml version="1.0" encoding="UTF-8"?>
  <step1>
...
</step1>

Please help.  Thank you,

Song,
Mark T. (Guest)
on 2009-04-29 05:31
(Received via mailing list)
On Apr 28, 8:34 pm, Song C. <removed_email_address@domain.invalid> wrote:
> </hash>
>
> I want to get rid of that <hash> so that it is just
> <?xml version="1.0" encoding="UTF-8"?>
>   <step1>
> ..
> </step1>

Since you want the xml declaration, I assume you want a valid XML
document. A valid XML document must have exactly one root element. You
cannot remove the root, because then if your hash had more than one
entry it would produce multiple roots and thus invalid XML.

You can change the name of the element, however:
hash.to_xml({:root => 'steps'})

-- Mark.
Song C. (Guest)
on 2009-04-29 19:30
(Received via mailing list)
Thank you!!!
THis helps!!

Song,
This topic is locked and can not be replied to.