SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Wednesday, September 1, 2010

Parsing XML in ruby using Hpricot

For parsing xml in one of my projects, i started surfing then i tried with hpricot.There are lots of parsers, but i just preferred to go with hpricot. First i installed the gem hpricot.

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.

Controller
class XmlController < ApplicationController
  #Module included
  include Parsexml
  
  def index
    parser('http://localhost:3000/users.xml')
  end
  
end
module
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

Views

Normal

<%(@xml/'user').each do |xml|%> <%=(xml/'firstname')%> <%end%>

XML

<%(@xml1/'user').each do |xml|%> <%=(xml/'Firstname')%> <%end%>
For Hpric Rdoc :http://rdoc.info/github/hpricot/hpricot/master/file/README.md