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

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?

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

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