For retrieving the xml, we write our logic inside the controller itself.As we are following dry concepts in rails, i had written the logic in the module and included the module in the controller.We have use 'open-uri', since it is an easy-to-use wrapper for net/http, net/https and net/ftp.It is possible to open http/https/ftp URL as usual like opening a file.
For ref : http://ruby-doc.org/stdlib/libdoc/open-uri/rdoc/classes/OpenURI.html
Finally the Hpricot() method takes a string or any IO object and loads the contents into a document object.Hpricot uses an internal buffer to parse the file, so the IO will stream properly and large documents won't be loaded into memory all at once. However, the parsed document object will be present in memory, in its entirety.
I have tried the two methods from Hpricot.First one is the normal one to load the xml,second one is Hpricot.XML().The primary way to use Hpricot's XML mode is to call the Hpricot.XML method.
class XmlController < ApplicationController #Module included include Parsexml def index parser('http://localhost:3000/users.xml') end endmodule
module Parsexml require 'open-uri' require 'hpricot' def parser(xml) @xml ||= Hpricot(open(xml)) @xml1=open(xml) do |f| Hpricot.XML(f) end end end ViewsFor Hpric Rdoc :http://rdoc.info/github/hpricot/hpricot/master/file/README.md
Normal<%(@xml/'user').each do |xml|%> <%=(xml/'firstname')%> <%end%>
XML<%(@xml1/'user').each do |xml|%> <%=(xml/'Firstname')%> <%end%>