# How to convert the hash to xml - "Not all elements respond to to\_xml"

**URL:** <https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358>\
**Category:** Rails\
**Created:** [November 28, 2008, 8:09am UTC](https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358 "2008-11-28T08:09:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [November 28, 2008, 8:09am UTC](https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358/1 "2008-11-28T08:09:46Z")

</div>

Hi,

I have a problem…  
I am Working on Rails with Flex.  
I am getting a xml from the flex form… I need to get the xml  
and  
has to insert the record  
The xml I am getting in the form as…  
  
  
  
100  
  
  
“xyz”

12-32-122/34   
 123 road   
  
 unknown   
 state   
 In   
 321456   
  
  
Developer  
  
.  
.

```
            </insert>
            <insert>
                  <number>
                            101
                 </number>
                 <name>
                             "pqr"
                 </name>
                  <address>
                              <street>

```

1-122/76   
 cross road  
  
  
 city   
 some state   
 US   
 321456   
  
  
Developer  
  
.  
.

```
            </insert>
                 .
                 .
                 .
     </root>

```

In these… each insert tag is a record which have to be entered  
into  
DB.  
But we are receiving the xml as HashWithIndifferentAccess  
params[:xml].class =\> HashWithIndifferentAccess

We tried converting into hash  
h = (params[:xml]).Hash  
puts “#{h.to\_xml}”

it is showing the error as “Not all elements respond to to\_xml”

How to achieve the insertion are there any ways to get the xml…

---

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [November 28, 2008, 10:41am UTC](https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358/2 "2008-11-28T10:41:01Z")

</div>

On Nov 28, 7:09 am, “hema gonaboina” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 100
> <city> unknown </city>
> </insert>
> 
> ```
> 
> 1-122/76   
> .  
> But we are receiving the xml as HashWithIndifferentAccess  
> params[:xml].class =\> HashWithIndifferentAccess

Rails parses the xml for you (into a hash). you can get the raw body  
of the request if you want (@request.body)

Fred

---

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [November 28, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358/3 "2008-11-28T12:25:20Z")

</div>

Frederick C. wrote:

> On Nov 28, 7:09ï¿½am, “hema gonaboina” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ 100  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ unknown   
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½   
> > 1-122/76   
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½.  
> > ï¿½ But we are receiving the xml as HashWithIndifferentAccess  
> > ï¿½ params[:xml].class =\> ï¿½HashWithIndifferentAccess
> 
> Rails parses the xml for you (into a hash). you can get the raw body  
> of the request if you want (@request.body)
> 
> Fred

Thank You Fred for your response

But, I didn’t get you.. Can U explain me with example.. I need the xml  
only but i am receiving object of class HashWithIndifferentAccess.. so i  
am trying to convert into pure Hash and then to xml..  
How to receive the value as xml ?  
If it works no need of conversion..  
How to use the @request.body ?

---

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [November 28, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358/4 "2008-11-28T12:31:27Z")

</div>

On Nov 28, 11:25 am, Hema G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## But, I didn’t get you.. Can U explain me with example.. I need the xml only but i am receiving object of class HashWithIndifferentAccess.. so i am trying to convert into pure Hash and then to xml.. How to receive the value as xml ? If it works no need of conversion.. How to use the @request.body ?

Don’t know how else to explain it - request.body ( my mistake - the @  
there is deprecated) is exactly the bytes that were sent to you.  
Trying to convert the hash back to xml seems completely circular (and  
HashWithIndifferentAccess is almost the same as Hash - the only  
difference is that you can use symbols or strings for the keys and  
they are considered identical)

Fred

---

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [November 28, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/how-to-convert-the-hash-to-xml-not-all-elements-respond-to-to-xml/154358/5 "2008-11-28T12:33:28Z")

</div>

Hema G. wrote:

> Frederick C. wrote:
> 
> > On Nov 28, 7:09ï¿½am, “hema gonaboina” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ 100  
> > > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ unknown   
> > > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½   
> > > 1-122/76   
> > > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½.  
> > > ï¿½ But we are receiving the xml as HashWithIndifferentAccess  
> > > ï¿½ params[:xml].class =\> ï¿½HashWithIndifferentAccess
> > 
> > Rails parses the xml for you (into a hash). you can get the raw body  
> > of the request if you want (@request.body)
> > 
> > Fred
> 
> Thank You Fred for your response
> 
> But, I didn’t get you.. Can U explain me with example.. I need the xml  
> only but i am receiving object of class HashWithIndifferentAccess.. so i  
> am trying to convert into pure Hash and then to xml..  
> How to receive the value as xml ?  
> If it works no need of conversion..  
> How to use the @request.body ?  
> I worked on it..  
> and found that it is collecting as hashes with arrays in between

```
 in out case it is as {"root" => {"insert"=> 

```

[{“name”=\>“xyz”,“address”=\>[{“city”=\>“city”,“state”=\>“state”…}],“job”=\>“developer”,…},{“name”=\>“pqr”,“address”=\>[{},{}…],..}]}}  
That is it takes arrays between the hashes..
