OK. I have come up to the final version that do what i want.
Here it is:
def parse_to_hash(node)
normalize(xml_to_hash_of_arrays(node))
end
def xml_to_hash_of_arrays(node)
hash = {}
REXML::XPath.each node, '*' do |node|
if node.parent.has_attributes?
node.parent.attributes.each_attribute do |attr|
hash[attr.name.underscore.to_sym] = attr.value
end
end
(hash[node.name.underscore.to_sym] ||= []) << if node.has_elements?
xml_to_hash_of_arrays(node)
else
(node.text || '').strip
end
end
return hash
end
def normalize(hash)
return unless hash.kind_of?(Hash)
hash.each do |key, value|
value.each { |val| normalize(val) }
hash[key] = *value
end
end
Many thanks Philip to your suggestions.
--
======================================================================
- Best regards, Nikolay Pavlov. <<<-----------------------------------
======================================================================