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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, > BillHi 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>>> 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 helpfulHere''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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Gustav Paul Your example has provided the help i needed and the problem got resolved. Thanks all for you response. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---