Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:38:08+00:00 2026-06-09T18:38:08+00:00

I have many levels of a nested hash like: { :foo => ‘bar’, :foo1

  • 0

I have many levels of a nested hash like:

 { :foo => 'bar', :foo1 => { :foo2 => 'bar2', :foo3 => 'bar3', :foo4 => { :foo5 => 'bar5' }}}

How can I convert them into an XML like this?:

<foo>bar</foo>
<foo1>
    <foo2>bar2</foo2>
    <foo3>bar3</foo3>
    <foo4>
      <foo5>bar5</foo5>
    </foo4>
</foo1>

I tried the xml.send method, but it converts the above nested hash to:

<foo1 foo3="bar3" foo4="foo5bar5" foo2="bar2"/>
<foo>bar</foo>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:38:10+00:00Added an answer on June 9, 2026 at 6:38 pm

    How about this?

    class Hash
      def to_xml
        map do |k, v|
          text = Hash === v ? v.to_xml : v
          "<%s>%s</%s>" % [k, text, k]
        end.join
      end
    end
    
    h.to_xml
    #=> "<foo>bar</foo><foo1><foo2>bar2</foo2><foo3>bar3</foo3><foo4><foo5>bar5</foo5></foo4></foo1>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchy of tables 3 levels deep (QualificaionType has many QualificationGroups, which
I have a three-level multi-nested form in Rails. The setup is like this: Projects
I have a large class with many nested subclasses of different types as follows:
I have data nested in to levels: L1 L2 x1 x2 x3 x4 A
I have sinned on so many levels. I'm hoping someone can tell me a
I have an array that is many levels deep and I am wondering what
I have a large SharePoint document library with many levels of subfolders in it,
I have an algorithm that uses deep recursion. How many levels deep the recursion
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have many different NSArray 's stored in .dat files, in the Documents folder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.