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

编程入门 行业动态 更新时间:2024-10-11 15:18:15
然后解析从Ruby on Rails中的API存储XML数据(Parse then Store XML Data from API in Ruby on Rails)

我知道有很多关于这个主题的条目,但我还没有看到关于如何存储数据的完整图片。 我正在尝试创建一个读取然后解析的系统,然后存储有关来自Yahoo Upcoming API的事件的信息。

url返回一个非常简单的xml,看起来像这样

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

我一直在阅读REXML和其他人,但我如何获取元素的价值并将它们存储到我的模型中? 目标是将XML中所有所需元素的值打印到文本框中以允许编辑数据,然后让用户保存在数据库中...我只是很难搞清楚如何做某事解析后的数据。

任何帮助,链接或建议都会对我有所帮助。

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.

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>

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解析为散列,然后使用散列创建模型?

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])

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])

更多推荐

本文发布于:2023-08-05 17:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   API   Ruby   Rails   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!