Created
December 15, 2011 15:27
-
-
Save wrburgess/1481481 to your computer and use it in GitHub Desktop.
Use Nokogiri to import an xml file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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