我想從互聯網上解析一個打開的數據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>
'children.css('geo')[「latitude」] .text' - 這顯然不是您訪問屬性值的方式。不過,我不知道什麼是正確的api。這取決於你:) –