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 3632820
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:34:26+00:00 2026-05-19T00:34:26+00:00

to_xml on Hash with string array fails with Not all elements respond to to_xml

  • 0

to_xml on Hash with string array fails with Not all elements respond to to_xml

 >>r={"records"=>["001","002"]}

 => {"records"=>["001", "002"]}

 >>r.to_xml

 RuntimeError: Not all elements respond
 to to_xml   from
/jruby/../1.8/gems/activesupport2.3.9/lib/active_support/core_ext/array/conversions.rb:163:in `to_xml'

Is there a rails preferred way to change the Hash.to_xml behavior to return

<records>
 <record>001</record>
 <record>002</record>
</records>

…

  • 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-05-19T00:34:27+00:00Added an answer on May 19, 2026 at 12:34 am

    Just like DigitalRoss said, this appears to work out of the box in Ruby 1.9 with ActiveSupport 3:

    ruby-1.9.2-p0 > require 'active_support/all'
     => true 
    ruby-1.9.2-p0 > r={"records"=>["001","002"]}
     => {"records"=>["001", "002"]} 
    ruby-1.9.2-p0 > puts r.to_xml
    <?xml version="1.0" encoding="UTF-8"?>
    <hash>
      <records type="array">
        <record>001</record>
        <record>002</record>
      </records>
    </hash>
    

    At least with MRI (you’re using JRuby, though), you can get similar behavior on Ruby 1.8 with ActiveSupport 2.3.9:

    require 'rubygems'
    gem 'activesupport', '~>2.3'
    require 'active_support'
    class String
      def to_xml(options = {})
        root = options[:root] || 'string'
        options[:builder].tag! root, self
      end
    end
    

    Which gives you…

    ruby-1.8.7-head > load 'myexample.rb'
     => true 
    ruby-1.8.7-head > r={"records"=>["001","002"]}
     => {"records"=>["001", "002"]} 
    ruby-1.8.7-head > puts r.to_xml
    <?xml version="1.0" encoding="UTF-8"?>
    <hash>
      <records type="array">
        <record>001</record>
        <record>002</record>
      </records>
    </hash>
    

    Note that my code doesn’t work with Ruby 1.9 and ActiveRecord 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did this: [User.first, User.last].to_xml and got this: <users type=array> <user> <created-at type=datetime>2010-03-16T06:40:51Z</created-at> <id
I need to create a keyed hash for a string of XML to send
I've got the following code to output a list of items from amazon, but
I need to pass (via string content of a HTTP request/response body) name value
Has anyone using .net actually worked out how to successfully sign a signature to
I am currently writing an XML parser that parses a lot of data, with
I'm trying to integrate Amazon Product API into my website and came across several
I have a program that handles byte arrays in Java, and now I would
I know this is a vague open ended question. I'm hoping to get some
I've got a method which currently takes an IList as a parameter, where UserBO

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.