Parsing XML Using Ruby on Rails

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

Kiran wrote:

Bill Walton 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

Hey

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

here's an excerpt from the above link:

require 'cobravsmongoose' xml = '<alice><bob>charlie</bob><bob>david</bob></alice>' 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 Paul

Kiran wrote:

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

Bill Walton wrote:

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

Kiran wrote:

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 Paul wrote:

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

here's an excerpt from the above link:

require 'cobravsmongoose' xml = '<alice><bob>charlie</bob><bob>david</bob></alice>' 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 Paul

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 "<foo><bar>hello</bar><baz>there</baz></foo>" => {"foo"=>{"baz"=>"there", "bar"=>"hello"}} >>

b