从API中的Ruby on Rails的解析然后存储XML数据 [英] Parse then Store XML Data from API in Ruby on Rails

查看:109
本文介绍了从API中的Ruby on Rails的解析然后存储XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有一吨关于此主题的作品,但我还没有看到完整的画面又如何存储数据。我试图做一个系统,然后读取解析然后存储有关从雅虎即将API事件的信息。

I know there have been a ton of entries about this topic but I haven't seen the complete picture yet of how to store the data. I am trying to make a system that reads then parses then stores information about events from the Yahoo Upcoming API.

该网址返回pretty简单的XML,看起来像这样

The url returns a pretty simple xml that looks like this

<event>
<id>489875230</id>
<description>Open Bar</description>
<status>Live</status>
<latitude>29.74</latitude>
<longitude>-95.37</longitude>
</event>

我一直在读入REXML和其他人,但我怎么走元素的值,并将其存储到我的模型?
我们的目标是打印从XML到文本框所需的全部元素中的值,以允许编辑的数据的话,那么让用户保存到数据库中...我只是我有一个很难搞清楚如何做有一次,该数据已解析。

I've been reading into REXML and others but how do I take the value of the elements and store them into my model? The goal is the print the values from all the desired elements in the XML to textboxes to allow the data to be edited then, then letting the user save in the database...I just am having a hard time figuring out how to do something with the data once its parsed.

任何帮助,链接,或建议将真正帮助我很多。

Any help, link, or suggestions would really help me out alot.

推荐答案

设置了相同属性的模型,解析XML到哈希,然后创建哈希的模式?

set up a model with the same attributes, parse the xml to a hash, then create the model with the hash?

class CreateEvent < ActiveRecord::Migration
  def self.up
    create_table :events do |t|
    t.id :id
    t.string :decription
    t.string :status
    t.decimal :latitude
    t.decimal :longitude      #( you'll want to set the precision and scale properly here)
  end
end


data = Hash.from_xml <<EOX
<event>
<id>489875230</id>
<description>Open Bar</description>
<status>Live</status>
<latitude>29.74</latitude>
<longitude>-95.37</longitude>
</event>
EOX

Event.create!(hash[:event])

这篇关于从API中的Ruby on Rails的解析然后存储XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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