title: XML import to PostgreSQL using Nokogiri
body: <p>I would like to import an XML file from a URL using Nokogiri and save it to my PostgreSQL database.</p>
  
  <p>In my schema.rb I have the following table:</p>
  
  <pre><code>create_table "centres", force: :cascade do |t|
    t.string   "name"
    t.string   "c_type"
    t.text     "description"
    t.float    "lat"
    t.float    "long"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end
  </code></pre>
  
  <p>Below is a sample from the file I am importing:</p>
  
  <pre><code>&lt;facility&gt;
  &lt;id&gt;CG432&lt;/id&gt;
  &lt;facility_name&gt;Cairncry Community Centre&lt;/facility_name&gt;
  &lt;expiration&gt;2099-12-31T23:59:59Z&lt;/expiration&gt;
  &lt;type&gt;Community Centre&lt;/type&gt;
  &lt;brief_description/&gt;
  &lt;lat&gt;57.1601027&lt;/lat&gt;
  &lt;long&gt;-2.1441739&lt;/long&gt;
  &lt;/facility&gt;
  </code></pre>
  
  <p>I created the following import.rake task in lib/tasks:</p>
  
  <pre><code>require 'rake' 
  require 'open-uri'
  require 'Nokogiri'
  
  namespace :db do 
      task :xml_parser =&gt; :environment do 
          doc = Nokogiri::XML(open("http://sample.xml")) 
          doc.css('centre').each do |node| 
                  facility_name = node.xpath("centre").text,
                  type = node.xpath("centre").text,
                  brief_description = node.xpath("centre").text,
                  lat = node.xpath("centre").text,
                  long = node.xpath("centre").text,               
  
                  Centre.create(:facility_name =&gt; name, :type =&gt; c_type, :brief_description =&gt; description, :lat =&gt; lat, :long =&gt; long)
              end
          end
      end
  </code></pre>
  
  <p>I tried <code>rake db:migrate</code> and also <code>rake -T | grep import</code>.</p>