2015-10-29 118 views
0

我想從互聯網上解析一個打開的數據XML文件到我的rails數據庫中。以下是代碼應該分析它:TypeError:沒有將字符串隱式轉換爲整數

require 'rake' 
require 'open-uri' 
namespace :db do 
    task :xml_parser => :environment do 
    doc = Nokogiri::XML(open("https://dl.dropboxusercontent.com/u/21695507/openplaques/gb_20151004.xml")) 
    doc.css('plaque').each do |node| 
     children = node.children 
     Plaque.create(
     :title => children.css('title').inner_text, 
     :subject => children.css('subjects').inner_text, 
     :colour => children.css('colour').inner_text, 
     :inscription => children.css('inscription raw').inner_text, 
     :latitude => children.css('geo')["latitude"].text, 
     :longitude => children.css('geo')["longitude"].text, 
     :address => children.css('address').inner_text, 
     :organisation => children.css('organisation').inner_text, 
     :date_erected => children.css('date_erected').inner_text 
    ) 
    end 
    end 
end 

這裏是架構:

create_table "plaques", force: :cascade do |t| 
    t.string "title" 
    t.string "subject" 
    t.string "colour" 
    t.text  "inscription" 
    t.string "latitude" 
    t.string "longitude" 
    t.text  "address" 
    t.text  "organisation" 
    t.string "date_erected" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
end 

我跑耙分貝:xml_parser,我得到以下錯誤:

TypeError: no implicit conversion of String into Integer 

以下是我試圖分析的XML文件中的一個示例。

<plaque uri="http://openplaques.org/plaques/4856" machine_tag="openplaques:id=4856" created_at="2010-11-26T13:58:23+00:00" updated_at="2011-06-28T17:00:01+01:00"> 
    <title>Arthur Linton blue plaque</title> 
    <subjects>Arthur Linton</subjects> 
    <colour>blue</colour> 
    <inscription> 
    <raw> 
     World Champion Cyclist 1895 lived here Arthur Linton 1872-1896 
    </raw> 
    <linked> 
     World Champion Cyclist 1895 lived here <a href="/people/2934">Arthur Linton</a> 1872-1896 
    </linked> 
    </inscription> 
    <geo reference_system="WGS84" latitude="51.7005" longitude="-3.4251" is_accurate="true" /> 
    <location> 
    <address>Sheppard's Pharmacy, 218 Cardiff Road</address> 
    <locality uri="http://0.0.0.0:3000/places/gb/areas/aberaman/plaques">Aberaman</locality> 
    <country uri="http://0.0.0.0:3000/places/gb">United Kingdom</country> 
    </location> 
    <organisation uri="http://0.0.0.0:3000/organisations/rhondda_cynon_taf_council">Rhondda Cynon Taf Council</organisation> 
    <date_erected>2009-10-26</date_erected> 
    <person uri="http://0.0.0.0:3000/people/2934">Arthur Linton</person> 
</plaque> 
+0

'children.css('geo')[「latitude」] .text' - 這顯然不是您訪問屬性值的方式。不過,我不知道什麼是正確的api。這取決於你:) –

回答

0

有一個更簡單的解決方案,完美的工作!

require 'rake' 
require 'open-uri' 

namespace :db do 
    task :xml_parser => :environment do 
     doc = Nokogiri::XML(open("https://dl.dropboxusercontent.com/u/21695507/openplaques/gb_20151004.xml")) 
     doc.css('plaque').each do |node| 
       title = node.xpath("plaque").text, 
       subject = node.xpath("plaque").text, 
       colour = node.xpath("plaque").text, 
       inscription = node.xpath("plaque").text, 
       latitude = node.xpath("plaque").text, 
       longitude = node.xpath("plaque").text, 
       address = node.xpath("plaque").text, 
       organisation = node.xpath("plaque").text, 
       date_erected = node.xpath("plaque").text 

       Plaque.create(:title => title, :subject => subject, :colour => colour, :inscription => inscription, :latitude => latitude, :longitude => longitude, :address => address, :organisation => organisation, :date_erected => date_erected) 
      end 
     end 
    end 
1

我不認爲錯誤是在架構或Place.create(...)內容。我認爲這是您從Nokogiri獲取數據的方式。 some_node.css("some-selector")將返回一組符合條件的多個節點。可能發生的情況是節點數爲1(或0),因此您的.inner_text調用起作用。

我相信你的問題是關於兩行獲取經緯度:

:latitude => children.css('geo')["latitude"].text, 
:longitude => children.css('geo')["longitude"].text, 

children.css('geo')將返回一組節點,在這種情況下,它類似於一個單一元素的數組[geo]。但是,您致電["latitude"]就像是要求一個數組的第latitude個元素......這是沒有意義的。具體而言,

a = ["a", "b", "c", "d"] 
a[1] # => "b" 
a["longitude"] # => what?!?, or TypeError: no implicit conversion of String into Integer 

我會做什麼,讓您的緯度和長期價值,首先拉出從css("geo")搜索的第一個元素。然後,調用屬性來獲取屬性散列。 然後,你可以通過字符串獲取"latitude""longitude",最後,你需要調用.value來獲取文本值。完整的,

:latitude => children.css('geo').first.attributes["latitude"].value, 
:longitude => children.css('geo').first.attributes["longitude"].value, 
+0

它幫助,但我接下來的事情是:NoMethorError:未定義的方法'屬性'爲零:NilClass – bystrik

+1

@BystrikOndica:也許是時候學習一些調試技巧。獲取錯誤?檢查值/變量。他們是你期望他們是什麼?如果不是,爲什麼?不斷問問題(對你自己)和偷看方案的狀態。閱讀文檔也有很大幫助。 –

相關問題