Parsing XML Using Ruby on Rails

Hi,

I am new to ruby on rails.

I need to parse and read xml response from third party url using ruby on
rails.

For example if i make a request to url like

www.testurl234455.com/test/getsomething?param1=abcd

the url returns an xml response. for example the xml returned is
something:

<?xml version="1.0" ?> tester1 24

I need read the name and age from the xml file after making request to
above url using ruby on rails.

Please help me. thanks in advance for your help

Kiran wrote:

I need to parse and read xml response from third party
url using ruby on rails.

REXML is part of the standard Rails distribution and is probably what
you’re
looking for.

hth,
Bill

Bill W. wrote:

Kiran wrote:

I need to parse and read xml response from third party
url using ruby on rails.

REXML is part of the standard Rails distribution and is probably what
you’re
looking for.

hth,
Bill

Hi Bill,

Aware that REXML can be used but unable to find how it can be used to
parse resposes from urls. Search in google for that hasnt beenmuch help
ful

Small code sippet or name of the method used to do that can be helpful

Thanks
Kiran

Aware that REXML can be used but unable to find how it can be used to
parse resposes from urls. Search in google for that hasnt beenmuch help
ful

Small code sippet or name of the method used to do that can be helpful

Here’s a tutorial:

http://www.germane-software.com/software/rexml/docs/tutorial.html

Basically you can just create a Document by passing in the xml as a
string (response.body probably). Then you get the root element and pull
what you want out of the doc via xpaths.

Or, if you just need to convert all the content into a nested hash, see
below.

Gustav P. wrote:

Cheers,
Gustav P.

That cobra thingy looks interesting… will have to check it out. But
you’re right that rails will do this now (I think added as part of the
REST support in 1.2).

Rails adds a from_xml class method to Hash:

~/rails/whitelabel $ script/console

Loading development environment.

h = Hash.from_xml “hellothere”
=> {“foo”=>{“baz”=>“there”, “bar”=>“hello”}}

b

Kiran wrote:

looking for.
parse resposes from urls. Search in google for that hasnt beenmuch help
ful

Small code sippet or name of the method used to do that can be helpful

Thanks
Kiran

Hey

Check out CobraVsMongoose:
http://cobravsmongoose.rubyforge.org

here’s an excerpt from the above link:

require ‘cobravsmongoose’
xml = ‘charliedavid’
CobraVsMongoose.xml_to_hash(xml)

=> { “alice” => { “bob” => [{ “$” => “charlie” }, { “$” => “david” }]

} }

friendly and easy to use. There will undoubtedly be similar projects out
there (possibly even something in rails?) but in my experience this has
been excellent.

eg:
require ‘open-uri’
response = open(“http://somesite.com”).read
arrays_of_hashes = CobraVsMongoose.xml_to_hash(response)

Cheers,
Gustav P.

Thanks Gustav P.

Your example has provided the help i needed and the problem got
resolved.

Thanks all for you response.