简体   繁体   中英

Parse then Store XML Data from API in Ruby on Rails

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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