Rails Nokogiri XML Parser

时间:2017-03-09 08:59:12

标签: ruby-on-rails xml

我是一名铁杆新手,并尝试通过nokogiri XML Parser工作。

我找到了许多有用的东西,但它从来没有完全为我而来。所以我想我会请求你的帮助,这是我的第一个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的rails控制台中运行并得到一点=&gt; 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/18074772@50x50.jpg</Image50>
        <Image100>http://c.cfjump.com/Products/24870/18074772@100x100.jpg</Image100>
        <Image120>http://c.cfjump.com/Products/24870/18074772@120x120.jpg</Image120>
        <Image200>http://c.cfjump.com/Products/24870/18074772@200x200.jpg</Image200>
        <Image300>http://c.cfjump.com/Products/24870/18074772@300x300.jpg</Image300>
        <Image400>http://c.cfjump.com/Products/24870/18074772@400x400.jpg</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>

期待您的反馈

1 个答案:

答案 0 :(得分: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