0
我目前有一個XML文件,除了一部分以外,它正確讀取。這是一個項目列表,有時一個項目有多個條形碼。在我的代碼中,它只提取第一個。我如何迭代多個條形碼。請看下面的代碼:用Nokogiri讀取XML文件
def self.pos_import(xml)
Plu.transaction do
Plu.delete_all
xml.xpath('//Item').each do |xml|
plu_import = Plu.new
plu_import.update_pointer = xml.at('Update_Type').content
plu_import.plu = xml.at('item_no').content
plu_import.dept = xml.at('department').content
plu_import.item_description = xml.at('item_description').content
plu_import.price = xml.at('item_price').content
plu_import.barcodes = xml.at('UPC_Code').content
plu_import.sync_date = Time.now
plu_import.save!
end
end
我的測試XML文件是這樣的:
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<items>
<Item>
<Update_Type>2</Update_Type>
<item_no>0000005110</item_no>
<department>2</department>
<item_description>DISC-ALCOHOL PAD STERIL 200CT</item_description>
<item_price>7.99</item_price>
<taxable>No</taxable>
<Barcode>
<UPC_Code>0000005110</UPC_Code>
<UPC_Code>1234567890</UPC_Code>
</Barcode>
</Item>
</Items>
任何想法如何拉兩個UPC_Code場出來,他們寫信給我的數據庫?
這仍然只是把第一個UPC_Code放到字段中。它不會將兩個UPC_Code字段放在那裏。 – 2014-09-05 16:43:52
xpath語句中有錯誤。它應該有兩個'''來查找所有的UPC_code標籤。 – infused 2014-09-05 18:00:25
我注意到了,並在我的代碼中改變了它,但沒有運氣。仍然只是第一個UPC_Code。 plu_import.barcodes = xml.xpath('// UPC_Code')。map(&:content) – 2014-09-05 18:31:55