导轨3.0解析XML和插入到数据库中 [英] Rails 3.0 Parsing XML and Inserting into Database
问题描述
我是新来的Rails,并正尝试我的应用程序挂接到第三方API(简化版,它有Rails的宝石或插件)。
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.
更新:
Rake任务:
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在我的code,只是藏从这个code粘贴。
这code不起作用。我不能为我的生命找出原因。我得到的是在终端话说这没救了,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:
我检查的网址是正确的,并已通过检查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 介绍教程。
这篇关于导轨3.0解析XML和插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!