I recently learned how to import XML feeds into rails using nokogiri with the following code.
Instead of downloading my feed from an URL, I have the option to access it directly from an URL.
let's say that URL is `www.feedurl.com'
how would I update the below code to get it from that URL:
class Product < ApplicationRecord
def self.xml_parser
doc = Nokogiri::XML(open("#{Rails.root}/datafeed.xml"))
frothieproducts = doc.xpath('//FeedItems/FeedItem')
frothieproducts.map do |feeditem|
product = Product.new
product.product_name = feeditem.xpath('Name').text
product.product_description = feeditem.xpath('Description').text
product.product_link = feeditem.xpath('Url').text
product.save!
end
end
end
If you want to access the xml data from www.feedurl.com
you can change this line:
doc = Nokogiri::XML(open("#{Rails.root}/datafeed.xml"))
to
doc = Nokogiri::XML(open("http://www.feedurl.com"))