From: Nikolay Pavlov Date: 2007-09-05T02:32:52+09:00 Subject: Re: Need help in parsing REXML::Document On Monday 03 September 2007 21:28:57 Phlip wrote: > Outside of that trick, my code is much more flabby that usual for Hash > contortions. Someone might be able to use .inject or something to get it > down to fewer lines, in one pass. Thanks Phlip. With your suggestions i have come to some thing like this at the end: 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| (hash[node.name.underscore.to_sym] ||= []) << if node.has_elements? parse_node(node) else (node.text || '').strip end end end def normalize(hash) return unless hash.kind_of?(Hash) hash.each do |key, value| value.each { |val| reduce(val) } hash[key] = *value end end But one thing is still missed. In my original example i want to move the attributes of the nodes (that hash_elements? == true) to the array, for example: v3 v4 Should produce: [{:id => "some id", :sid => "some sid", :trans => "v4", :data => "v4"}] -- ====================================================================== - Best regards, Nikolay Pavlov. <<<----------------------------------- ======================================================================