views:

26

answers:

2

I need to access xml that is located in another web server. Does rails/ruby have a function that allows me to access this xml and then may be store it in a variable so that i can process it with libxml-ruby?

+1  A: 

There are a number of Ruby-level libraries to access remote HTTP resources. The oldest is Net::HTTP

Once you've received the xml, use Hpricot or other options to parse it.

Added:

Remember that you probably don't want to make your clients wait while your Rails server queries another server. Cache your XML results if you can, or fetch the XML in the background.

Larry K
+1  A: 

If you're not making DOM modifications (that is, you don't need the XML structure but rather the data in it) you can use HTTParty, which does all the parsing for you.

For example:

timeline = HTTParty.get("http://twitter.com/statuses/public_timeline.xml", :format => :xml)
timeline["statuses"].map{|status| status["user"]["screen_name"] } # => ["bobby", "johnny", "denisss"]
Chubas