Forum: Ruby on Rails Parsing XML Using Ruby on Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kiran (Guest)
on 2007-03-25 22:40
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" ?>
<DATA>
  <test>
    <person>
      <name>tester1</name>
      <age>24</age>
    </person>
  </test>
</DATA>

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
Bill W. (Guest)
on 2007-03-25 23:11
(Received via mailing list)
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 (Guest)
on 2007-03-25 23:32
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
Gustav P. (Guest)
on 2007-03-25 23:55
(Received via mailing list)
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 = '<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 P.
Ben M. (Guest)
on 2007-03-26 00:45
(Received via mailing list)
>>
>> 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/doc...

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

b
Kiran (Guest)
on 2007-03-26 03:36
Thanks Gustav P.

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

Thanks all for you response.
This topic is locked and can not be replied to.