At each iteration, you discard the old content of the variable and
replace it
with a new hash. This way, at the end of the cycle, the @xml_data will
be a
hash containing only the text of the last node.
To do what you want, you should set @xml_data to an empty hash outside
the
outer each, then store the new node in the hash at each iteration:
def extract(xml, path)
require ‘rexml/document’
xml = xml.to_s.gsub(“utf-16”, “utf-8”)
doc = REXML::Document.new(xml) @xml_data = {}
doc.root.each_element(path) do |element|
element.each_element do |node|
puts “name: #{node.name} text: #{node.text}” @xml_data[node.name] => node.text}
end
end
return @xml_data
end
By the way, are you sure @xml_data needs to be an instance variable?
From the
way you use it in the piece of code you posted, it seems it should be a
local
variable.