简体   繁体   English

TypeError:没有将String隐式转换为Integer

[英]TypeError: no implicit conversion of String into Integer

I am trying to parse an open data XML file from the internet into my rails database. 我正在尝试将来自Internet的开放数据XML文件解析到我的rails数据库中。 Following is the code which should parse it: 以下是应解析它的代码:

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

And here is the schema: 这是模式:

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

I run rake db:xml_parser and I get the following error: 我运行rake db:xml_parser并得到以下错误:

TypeError: no implicit conversion of String into Integer

Below is a sample from the XML file I am trying to parse. 以下是我尝试解析的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>

I don't think the error is in the schema or the Place.create(...) content . 我不认为该错误发生在架构或Place.create(...) 内容中 I think it's the way you are fetching data out of Nokogiri. 我认为这是您从Nokogiri提取数据的方式。 some_node.css("some-selector") is going to return a set of multiple nodes that match the criteria. some_node.css("some-selector")将返回一组符合条件的多个节点。 It may so happen that the number of nodes is 1 (or 0), so your .inner_text call works. 节点数可能是1(或0),因此您的.inner_text调用有效。

I believe your issue is on the two lines for fetching latitude and longitude: 我相信您的问题在获取纬度和经度的两行中:

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

children.css('geo') will return a set of nodes, in this case it's similar to a single element array [geo] . children.css('geo')将返回一组节点,在这种情况下,它类似于单个元素数组[geo] However, your call to ["latitude"] is like asking an array for it's latitude -th element... which doesn't make sense. 但是,您对["latitude"]调用就像在向数组查询其latitude -th元素...这没有任何意义。 Concretely, 具体来说,

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

What I would do to get your lat and long values, is first pull out the first element from the css("geo") search. 我要做的是获取经度和纬度值,首先要从css("geo")搜索中提取第一个元素。 Then, call attributes to get the attributes hash. 然后,调用属性以获取属性哈希。 Then , you can fetch via string for "latitude" and "longitude" , and finally, you need to call .value to get the text value. 然后 ,您可以通过字符串获取"latitude""longitude" ,最后,您需要调用.value以获取文本值。 In full, 在全,

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

There was a simpler solution, which worked perfectly! 有一个更简单的解决方案,效果很好!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM