require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML::Document for the page we’re interested in...
@doc = Nokogiri::XML(File.open("data.xml"))
# Search for nodes by css
ids = []
@doc.xpath('//itemid').each do |link|
ids << link.content
end
hash = {}
i = 0
@doc.xpath('//realestate').each do |link|
hash.store(link.to_s)
i+=1
#p hash
#sleep 2
#break if i ==1
end
p hash
一切工作正常,除了hash.store,想我要的是「數據存儲與hash_id哈希裏面..謝謝如何陣列的內容存儲在一個哈希與hash_id
通常人們會使用散列存儲鍵/值對。在你的情況下,你只存儲link.to_s的值。什麼是散列的需要? –
我嘗試使用ruby代碼** hash.store(id,link.to_s)**,但它顯示錯誤。 –
'hash.store(key,value)'與'hash [key] =(value)'相同。只需使用'[]'而不是'#store'。錯誤應該是你沒有提供鍵和值的參數。 – nicooga