# Xml to hash/array and back to xml again with xml-simple?

**URL:** <https://www.ruby-forum.com/t/xml-to-hash-array-and-back-to-xml-again-with-xml-simple/120240>\
**Category:** Rails\
**Created:** [November 15, 2007, 3:00am UTC](https://www.ruby-forum.com/t/xml-to-hash-array-and-back-to-xml-again-with-xml-simple/120240 "2007-11-15T03:00:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 15, 2007, 3:00am UTC](https://www.ruby-forum.com/t/xml-to-hash-array-and-back-to-xml-again-with-xml-simple/120240/1 "2007-11-15T03:00:21Z")

</div>

Im trying to create an xmlrpc service. Rails by default uses xml\_simple  
to parse incoming raw xml data into a hash.

The problem is, that if I start out with a hash like this:

hash = {:books =\> [{:id=\>4,:key=\>3}, {:id=\>644, :key=\>54}]} and convert  
it to xml via to\_xml, when I read it back in, it ends up doing something  
like:

result\_hash = {:books =\> {“book” =\> [{…},{…}}]}

so I end up accessing the books array with hash[“books”][“book”]

Has anyone experienced this before?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 15, 2007, 5:26am UTC](https://www.ruby-forum.com/t/xml-to-hash-array-and-back-to-xml-again-with-xml-simple/120240/2 "2007-11-15T05:26:30Z")

</div>

Aryk G. wrote:

> Im trying to create an xmlrpc service. Rails by default uses xml\_simple  
> to parse incoming raw xml data into a hash.
> 
> The problem is, that if I start out with a hash like this:
> 
> hash = {:books =\> [{:id=\>4,:key=\>3}, {:id=\>644, :key=\>54}]} and convert  
> it to xml via to\_xml, when I read it back in, it ends up doing something  
> like:
> 
> result\_hash = {:books =\> {“book” =\> [{…},{…}}]}
> 
> so I end up accessing the books array with hash[“books”][“book”]
> 
> Has anyone experienced this before?

Yes. Rails does some extra post-processing on the xml-simple results to  
get something that works. What version of Rails are you using? Some of  
that behavior changed a few months ago.

I like the description of xml-simple as neither xml nor simple. It seems  
to work ok for REST and ARes though. But if you’re doing xmlrpc you  
should be using a real xmlrpc package.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 15, 2007, 6:45am UTC](https://www.ruby-forum.com/t/xml-to-hash-array-and-back-to-xml-again-with-xml-simple/120240/3 "2007-11-15T06:45:38Z")

</div>

Im using rails 1.2.5, is there more improvements on that?

Also, which package do you recommend. The thing is, is that many of  
these api/service methods have a respond\_to block which implements other  
mime-types. Now I can do format.xml{render :xml =\> “”}, but im not  
sure if i can do that if i use a package, can I?

Josh S. wrote:

> Aryk G. wrote:
> 
> > Im trying to create an xmlrpc service. Rails by default uses xml\_simple  
> > to parse incoming raw xml data into a hash.
> > 
> > The problem is, that if I start out with a hash like this:
> > 
> > hash = {:books =\> [{:id=\>4,:key=\>3}, {:id=\>644, :key=\>54}]} and convert  
> > it to xml via to\_xml, when I read it back in, it ends up doing something  
> > like:
> > 
> > result\_hash = {:books =\> {“book” =\> [{…},{…}}]}
> > 
> > so I end up accessing the books array with hash[“books”][“book”]
> > 
> > Has anyone experienced this before?
> 
> Yes. Rails does some extra post-processing on the xml-simple results to  
> get something that works. What version of Rails are you using? Some of  
> that behavior changed a few months ago.
> 
> I like the description of xml-simple as neither xml nor simple. It seems  
> to work ok for REST and ARes though. But if you’re doing xmlrpc you  
> should be using a real xmlrpc package.
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
