Nokogiri Xml Parsing


Q: How to parse Xml Document with Nokogiri?

Answer: In model:

1) Put this line in your gem file.

 gem 'nokogiri'

2) Require these after installing nokogiri gem in you project.


require 'open-uri' # required for this
require 'net/http' # not for this its used in my project
require 'uri'#not required for this [Its just used in my project]
require 'rubygems' #no need of it in rails project as Gems are automatically loaded.
require 'nokogiri' #no need of it in rails project as Gems are automatically loaded.

3) Write this method in your model


def self.xml_parser
    xml_string = open("#{Rails.root}/abc.xml").read
    root = Nokogiri::XML(xml_string)
    hotels = root.xpath("hotels/hotel")
    hotels.each do |hotel|
    hotel_hash = {
        name: hotel.at_xpath("hotel_name").text,
        telephone_no:  hotel.at_xpath("hotel_phone_number").text,
        latitude: hotel.at_xpath("hotel_latitude").text,
        longitude: hotel.at_xpath("hotel_longitude").text,
        email: hotel.at_xpath("hotel_email").text
     }
        hotel_record = Hotel.create!(hotel_hash)
        hotel_record.save

    end
end

4) In rails console call this

  

ModelName.xml_parser
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: , ,
Posted in Nokogiri

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: