# Hash to\_xml from\_xml

**URL:** <https://www.ruby-forum.com/t/hash-to-xml-from-xml/186003>\
**Category:** Ruby\
**Created:** [March 30, 2010, 10:00am UTC](https://www.ruby-forum.com/t/hash-to-xml-from-xml/186003 "2010-03-30T10:00:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradphelan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aeb1de/32.png) [@bradphelan](https://www.ruby-forum.com/u/bradphelan)\
**Post date:** [March 30, 2010, 10:00am UTC](https://www.ruby-forum.com/t/hash-to-xml-from-xml/186003/1 "2010-03-30T10:00:59Z")

</div>

Am I missing something

```
require 'active_record'
require 'active_support'
require 'pp'

v = Hash.from_xml %{
<hash>
  <users type="array">
      <user type="integer">1</user>
      <user type="integer">2</user>
      <user type="integer">3</user>
  </users>
</hash>
}
pp v
pp v.to_xml

```

generates

```
{"hash"=>{"users"=>[1, 2, 3]}}

/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/

```

core\_ext/array/conversions.rb:163:in `to_xml': Not all elements respond to to_xml (RuntimeError) from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/ active_support/core_ext/hash/conversions.rb:115:in`to\_xml’  
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/  
active\_support/core\_ext/hash/conversions.rb:110:in `each' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/ active_support/core_ext/hash/conversions.rb:110:in`to\_xml’  
from /usr/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/  
xmlbase.rb:134:in `call' from /usr/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/ xmlbase.rb:134:in`\_nested\_structures’  
from /usr/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/  
xmlbase.rb:58:in `method_missing' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/ active_support/core_ext/hash/conversions.rb:109:in` **send** ’  
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/  
active\_support/core\_ext/hash/conversions.rb:109:in `to_xml' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/ active_support/core_ext/hash/conversions.rb:113:in`to\_xml’  
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/  
active\_support/core\_ext/hash/conversions.rb:110:in `each' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/ active_support/core_ext/hash/conversions.rb:110:in`to\_xml’  
from /usr/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/  
xmlbase.rb:134:in `call' from /usr/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/ xmlbase.rb:134:in`\_nested\_structures’  
from /usr/lib/ruby/gems/1.8/gems/builder-2.1.2/lib/builder/  
xmlbase.rb:58:in `method_missing' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/ active_support/core_ext/hash/conversions.rb:109:in` **send** ’  
from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/  
active\_support/core\_ext/hash/conversions.rb:109:in `to\_xml’

Any way to get this working?

Brad

---

<div class="post-metadata">

**Author:** ![bradphelan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/aeb1de/32.png) [@bradphelan](https://www.ruby-forum.com/u/bradphelan)\
**Post date:** [March 30, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/hash-to-xml-from-xml/186003/2 "2010-03-30T12:49:06Z")

</div>

bradphelan wrote:

> ```
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/
> 
> ```
> 
> core\_ext/array/conversions.rb:163:in `to\_xml’: Not all elements  
> respond to to\_xml (RuntimeError)

Indeed: ActiveSupport is trying to call to\_xml on each element of the  
array, and failing.

> > require ‘rubygems’  
> > =\> true  
> > require ‘active\_support’  
> > =\> true  
> > {}.to\_xml  
> > =\> “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>\n\n\n”  
> > 1.to\_xml  
> > NoMethodError: undefined method `to\_xml’ for 1:Fixnum  
> > from (irb):4

> Any way to get this working?

Googling “rails to\_xml”, the second hit is  
[http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html](http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html)  
I also found  
[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Conversions.html#M001207](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Conversions.html#M001207)

This suggests it’s only intended for use with ActiveRecord objects and  
collections of ActiveRecord objects, but you may need to dig further  
into the code. Perhaps have a look at ActiveResource.

However, these are all parts of Rails, and Rails questions are best  
asked in a Rails forum.
