2017-03-09 13 views
0

Ich bin ein Rails Newbie und versuche, meinen Weg durch einen Nokogiri XML Parser zu arbeiten.Rails Nokogiri XML Parser

Ich fand viele hilfreiche Sachen, aber für mich kam es nie komplett zusammen. Also ich dachte, ich würde um deine Hilfe bitten, das ist meine erste Stackoverflow-Frage. so hier geht:

In meinem Produktmodell definiere ich als

class Product < ApplicationRecord 
    def self.xml_parser 
     xml_string = open("#{Rails.root}/datafeed.xml").read 
     doc = Nokogiri::XML(xml_string) 
     frothieproducts = doc.xpath('//FeedItems/FeedItem') 
     frothieproducts.each do |feeditem| 
      product.product_name = feeditem.xpath('Name').text 
      product.product_description = feeditem.xpath('Description').text 
      product.product_link = feeditem.xpath('Url').text 
     end 
    end 
end 

Dann folge ich in Schienen Konsole mit Product.xml_parser laufen und ein wenig => 0

meiner XML-Datei wie so aussieht bekommen und datafeed.xml benannt und in der obersten Ebene

<FeedItems> 
    <FeedItem> 
     <MerchantId>24870</MerchantId> 
     <MerchantCampaignName>Froothie</MerchantCampaignName> 
     <DateCreated>2016-02-04T18:43:00.787</DateCreated> 
     <DateModified>2016-02-04T18:43:00.787</DateModified> 
     <SKU>400BLK</SKU> 
     <Name>The OPTIMUM 400</Name> 
     <Category>The OPTIMUM 400 - Revolutionary Cold Press Juicer (Black)</Category> 
     <Description>The OPTIMUM 400 - Revolutionary Cold Press Juicer (Black)</Description> 
     <Url>https://t.cfjump.com/13467/p/18074772</Url> 
     <OriginalUrl>http://www.froothie.com.au/store/optimum-juicer/optimum-slow-juicer</OriginalUrl> 
     <Image>http://c.cfjump.com/Products/24870/18074772.jpg</Image> 
     <Image50>http://c.cfjump.com/Products/24870/[email protected]</Image50> 
     <Image100>http://c.cfjump.com/Products/24870/[email protected]</Image100> 
     <Image120>http://c.cfjump.com/Products/24870/[email protected]</Image120> 
     <Image200>http://c.cfjump.com/Products/24870/[email protected]</Image200> 
     <Image300>http://c.cfjump.com/Products/24870/[email protected]</Image300> 
     <Image400>http://c.cfjump.com/Products/24870/[email protected]</Image400> 
     <Price>449</Price> 
     <Brand></Brand> 
     <Colour>Black</Colour> 
     <Currency>AUD</Currency> 
     <DeliveryCost></DeliveryCost> 
     <DeliveryTime></DeliveryTime> 
     <Features></Features> 
     <Gender></Gender> 
     <Genre></Genre> 
     <Keywords></Keywords> 
     <ContentRating></ContentRating> 
     <ModelNumber></ModelNumber> 
     <Platform></Platform> 
     <PriceRrp>449</PriceRrp> 
     <PriceSale></PriceSale> 
     <PromoText></PromoText> 
     <Size></Size> 
     <StockLevel></StockLevel> 
     <SubCategory></SubCategory> 
     <Custom1></Custom1> 
     <Custom2></Custom2> 
     <Custom3></Custom3> 
     <Custom4></Custom4> 
    </FeedItem> 

Wir freuen uns auf Ihr Feedback

Antwort

0

Ihr Parsing-Code ist in Ordnung, aber Sie sollten Array von Produkten zurückgeben. Ich sehe nicht, wie Sie product Variable deklarieren. Dieser Code gibt ein Array von Produkten zurück.

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 
+0

Hallo Alex, danke dafür, half definitiv die Dinge zusammen. Ich kann sehen, dass es die URL gezogen hat, aber sonst nichts .... es hat keine Produkte in meiner Datenbank gespeichert. –

+0

Ja, es gibt nur die Liste der Produktobjekte zurück. Ich habe die Antwort aktualisiert, um das Produkt in db zu speichern. Probieren Sie es jetzt. –

+0

Alex, du bist fantastisch, am besten, um auf –