XML API Product FEED

1

I want to develop a product feed with my suppliers. This is the XML that the API gives me with the available or exhausted products of this distributor to obtain general information about the product but mainly stock, sku, price ...

<Item>
  <PartID>203237</PartID>
  <Sku>STCK-11-R8</Sku>
  <Desc>Nite Ize Steelie Car Mount Kit</Desc>
  <DescExt>
    long description... blah blah
  </DescExt>
  <CategoryID>575</CategoryID>
  <CategoryName>NITE IZE STEELIE</CategoryName>
  <PhoneMake>UNIVERSAL</PhoneMake>
  <PhoneModel>UNIVERSAL</PhoneModel>
  <Status>1</Status>
  <Cost>17.50</Cost>
  <ImageUrl>
    http://www.myvoicecomm.com/core_files/uploads/STCK-11-R8.jpg
  </ImageUrl>
  <ImageFile>STCK-11-R8.jpg</ImageFile>
  <OnHand>125</OnHand>
  <Upc>094664027312</Upc>
  <Ean/>
  <Manufacturer/>
  <Mpn>STCK-11-R8</Mpn>
  <Msrp>34.99</Msrp>
  <OptionalImages>STCK-11-R8.jpg,STCK-11-R82.jpg,STCK-11-R83.jpg</OptionalImages>
  <EtaDate/>
  <ReplacementSku/>
  <PreviousSku/>
  <FirstReceived>04/18/2013</FirstReceived>
</Item>

I would like to be able to parsed the XML in a hash or array in order to manipulate it and save it in the database. The idea is to synchronize the number of times per day that is eventually defined.

They are around 1000 records, so they go in and out depending on the stock or they are discontinued. That is simply known if they have stock or if it is in zeros.

    
asked by Mauricio Lancheros 27.07.2017 в 19:57
source

2 answers

0

You could parse the XML in a hash with your answer with

hash = Hash.from_xml(respuesta)

If your answer contains spaces or line breaks you should replace them before.

hash = Hash.from_xml(respuesta.gsub("\n", ""))
    
answered by 27.07.2017 в 22:19
0

I would do it this way:

Hash.from_xml(response)

This will return a hash to you; for example, this would be the result of the XML that you provide:

{"Item"=>
  {"PartID"=>"203237",
   "Sku"=>"STCK-11-R8",
   "Desc"=>"Nite Ize Steelie Car Mount Kit",
   "DescExt"=>"\n    long description... blah blah\n  ",
   "CategoryID"=>"575",
   "CategoryName"=>"NITE IZE STEELIE",
   "PhoneMake"=>"UNIVERSAL",
   "PhoneModel"=>"UNIVERSAL",
   "Status"=>"1",
   "Cost"=>"17.50",
   "ImageUrl"=>
    "\n    http://www.myvoicecomm.com/core_files/uploads/STCK-11-R8.jpg\n  ",
   "ImageFile"=>"STCK-11-R8.jpg",
   "OnHand"=>"125",
   "Upc"=>"094664027312",
   "Ean"=>nil,
   "Manufacturer"=>nil,
   "Mpn"=>"STCK-11-R8",
   "Msrp"=>"34.99",
   "OptionalImages"=>"STCK-11-R8.jpg,STCK-11-R82.jpg,STCK-11-R83.jpg",
   "EtaDate"=>nil,
   "ReplacementSku"=>nil,
   "PreviousSku"=>nil,
   "FirstReceived"=>"04/18/2013"}
}
    
answered by 27.07.2017 в 22:20