2017-03-09 53 views
0

我是一名Rails新手,試圖通過nokogiri XML Parser工作。Rails Nokogiri XML分析器

我發現了很多有用的東西,但它從來沒有完全爲我完成。所以我想我會尋求你的幫助,這是我的第一個stackoverflow問題。所以這裏有雲:

在我的產品型號,我定義如下

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 

然後,我與Product.xml_parser軌控制檯運行,並得到一點點=> 0

我的XML文件看起來像這樣並命名爲datafeed.xml並在頂層

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

期待您的反饋

回答

0

你的解析代碼是好的,但你應該返回產品數組。我看不到你如何聲明product變量。 此代碼將返回一組產品。

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

嗨亞歷克斯,謝謝你,肯定有幫助。我可以看到它拉的URL,但沒有別的....它沒有保存任何產品到我的分貝。 –

+0

是的,它只是返回產品對象列表。我已經更新了答案,以便將產品保存在db中。現在就試試。 –

+0

亞歷克斯,你真棒,最好的東西醒來 –