Rails 3.0 解析 XML 并插入数据库 [英] Rails 3.0 Parsing XML and Inserting into Database

查看:16
本文介绍了Rails 3.0 解析 XML 并插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 新手,正在尝试将我的应用程序连接到第三方 API(它没有用于 Rails 的 gem 或插件).

I'm new to Rails, and am trying to hook up my application to a third-party API (it does't have a gem or plugin for Rails).

理想情况下,我希望能够做的是解析数据(我听说过关于 Nokogiri 的好消息,但不知道如何将其用于我想做的事情.不是因为缺乏尝试),然后插入到数据库中.

Ideally, what I want to be able to do is parse the data (I've heard good things about Nokogiri, but don't know how to use it for what I want to do do. not for lack of trying), and then insert it into the database.

有人可以提供说明或指出正确的方向吗?干杯.

Could anybody provide instructions or point me in the right direction? Cheers.

更新:

耙任务:

task :fetch_flyers => :environment do

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(open(url))

  events = doc.search('//event')

    events.each do |event|
      @data = Event.new(
        :name           => event.at('name').text,
        :date           => '2011-09-18',
        :time           => '17:00',
        :description    => event.at('long_description').text,
        :address        => event.at('street').text,
        :postcode       => event.at('postcode').text,   
        :price          => event.at('costs').text,
        :user_id        => 1,
        :genre_id       => 1,   
        :town_id        => 1)

    @data.save

    if @data.save
        puts "Success"
    else
        puts "This didn't save, F***"
    end
    end
end

我已经在我的代码中指定了 URL,只是在此代码粘贴中隐藏了它.此代码不起作用.我不能为我的生活弄清楚为什么.我得到的只是终端中的输出说这没有保存,F** 这意味着由于某种原因事件没有被保存.有人能摆脱一些吗?对此有所了解吗?

I've specified the URL in my code, just hidden it from this code paste. This code does not work. I can not for the life of me figure out why. All I get is the output in terminal saying "This didn't save, F** which means that for some reason the Events are not being saved. Could anyone shed some light on this?

更新 2:

我已检查 URL 是否正确,并已使用以下方法检查 XML 是否被正确解析:

I've checked the URL is correct, and have checked the XML is being parsed correctly by using:

# Printing Out the Variables to make sure they work.
    puts @name
    puts @date
    puts @time
    puts @desc
    puts @address
    puts @postcode
    puts @price
    puts @user
    puts @genre
    puts @town

.. 成功打印出终端中的值.但是,它仍然不会插入到我的数据库中.

..Which successfully prints out the values in terminal. However, it still won't insert into my database.

我的模型如下:

belongs_to :user
    belongs_to :genre
    belongs_to :town

    has_attached_file :image, :styles => { :grid => '90x128#', :list => '140x200#', :full => '400x548'}
    validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']  
    before_post_process :normalise_file_name

  validates :name, :presence => true
  validates :date, :presence => true
  validates :time, :presence => true
  validates :description, :presence => true
  validates :address, :presence => true
  validates :town, :presence => true
  validates :postcode, :presence => true
  validates :price, :presence => true
  validates :user_id, :presence => true
  validates :viewcount, :presence => true

我的 Development.log 文件只显示了以下内容:

My Development.log file just shows a load of:

  [1m[35mTown Load (0.1ms)[0m  SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
      [1m[36mTown Load (0.1ms)[0m  [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
      [1m[35mTown Load (0.1ms)[0m  SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
      [1m[36mTown Load (0.1ms)[0m  [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
      [1m[35mTown Load (0.1ms)[0m  SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
      [1m[36mTown Load (0.1ms)[0m  [1mSELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1[0m
      [1m[35mTown Load (0.1ms)[0m  SELECT "towns".* FROM "towns" WHERE "towns"."id" = 1 LIMIT 1
    .......

每次我尝试运行 rake 任务时.这有什么意义吗?

Every time I try to run the rake task. Does this mean anything?

推荐答案

使用 nokogiri 时,您可以指定 css 或 xpath 选择器,例如:

When using nokogiri you can specify css or xpath selectors, like:

doc = Nokogiri::XML(xml_string)
events = doc.search('//event')
events.each do |event|
  puts event.at('short_description')
end

另外,查看 nokogiri 介绍教程.

Also, check out nokogiri intro tutorials.

这篇关于Rails 3.0 解析 XML 并插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆