Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wrburgess
Created December 15, 2011 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrburgess/1481481 to your computer and use it in GitHub Desktop.
Save wrburgess/1481481 to your computer and use it in GitHub Desktop.
Use Nokogiri to import an xml file
require 'rubygems'
require 'nokogiri'
require 'open-uri'
Model.delete_all #if refreshing
doc = Nokogiri::XML(File.read("app/assets/files/example.xml"))
@doc = doc.xpath('//node_name').each do |record|
Model.create(
:column1 => record.at('@element_id').text,
:column2 => record.at('node_name').text,
:date => DateTime.strptime(record.at('date_node').text, "%m/%d/%Y"))
end
flash[:notice] = "File has been processed successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment