parsing nested XML nodes in Ruby <p>Hello I am trying get all the possible nested hierarchy key value of following XML. </p> <p>I want to create HTML for each node_name with some content and i want hierarchy menu.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;taxonomies&gt; &lt;taxonomy&gt; &lt;taxonomy_name&gt;World&lt;/taxonomy_name&gt; &lt;node atlas_node_id = "355064" ethyl_content_object_id="82534" geo_id = "355064"&gt; &lt;node_name&gt;Africa&lt;/node_name&gt; &lt;node atlas_node_id = "355611" ethyl_content_object_id="3210" geo_id = "355611"&gt; &lt;node_name&gt;South Africa&lt;/node_name&gt; &lt;node atlas_node_id = "355612" ethyl_content_object_id="35474" geo_id = "355612"&gt; &lt;node_name&gt;Cape Town&lt;/node_name&gt; &lt;node atlas_node_id = "355613" ethyl_content_object_id="" geo_id = "355613"&gt; &lt;node_name&gt;Table Mountain National Park&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355614" ethyl_content_object_id="" geo_id = "355614"&gt; &lt;node_name&gt;Free State&lt;/node_name&gt; &lt;node atlas_node_id = "355615" ethyl_content_object_id="1000550692" geo_id = "355615"&gt; &lt;node_name&gt;Bloemfontein&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355616" ethyl_content_object_id="" geo_id = "355616"&gt; &lt;node_name&gt;Gauteng&lt;/node_name&gt; &lt;node atlas_node_id = "355617" ethyl_content_object_id="37710" geo_id = "355617"&gt; &lt;node_name&gt;Johannesburg&lt;/node_name&gt; &lt;/node&gt; &lt;node atlas_node_id = "355618" ethyl_content_object_id="1000548256" geo_id = "355618"&gt; &lt;node_name&gt;Pretoria&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355619" ethyl_content_object_id="" geo_id = "355619"&gt; &lt;node_name&gt;KwaZulu-Natal&lt;/node_name&gt; &lt;node atlas_node_id = "355620" ethyl_content_object_id="43725" geo_id = "355620"&gt; &lt;node_name&gt;Durban&lt;/node_name&gt; &lt;/node&gt; &lt;node atlas_node_id = "355621" ethyl_content_object_id="1000576780" geo_id = "355621"&gt; &lt;node_name&gt;Pietermaritzburg&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355622" ethyl_content_object_id="" geo_id = "355622"&gt; &lt;node_name&gt;Mpumalanga&lt;/node_name&gt; &lt;node atlas_node_id = "355623" ethyl_content_object_id="67561" geo_id = "355623"&gt; &lt;node_name&gt;Kruger National Park&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355624" ethyl_content_object_id="" geo_id = "355624"&gt; &lt;node_name&gt;The Drakensberg&lt;/node_name&gt; &lt;node atlas_node_id = "355625" ethyl_content_object_id="" geo_id = "355625"&gt; &lt;node_name&gt;Royal Natal National Park&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355626" ethyl_content_object_id="" geo_id = "355626"&gt; &lt;node_name&gt;The Garden Route&lt;/node_name&gt; &lt;node atlas_node_id = "355627" ethyl_content_object_id="" geo_id = "355627"&gt; &lt;node_name&gt;Oudtshoorn&lt;/node_name&gt; &lt;/node&gt; &lt;node atlas_node_id = "355628" ethyl_content_object_id="" geo_id = "355628"&gt; &lt;node_name&gt;Tsitsikamma Coastal National Park&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355629" ethyl_content_object_id="3263" geo_id = "355629"&gt; &lt;node_name&gt;Sudan&lt;/node_name&gt; &lt;node atlas_node_id = "355630" ethyl_content_object_id="" geo_id = "355630"&gt; &lt;node_name&gt;Eastern Sudan&lt;/node_name&gt; &lt;node atlas_node_id = "355631" ethyl_content_object_id="" geo_id = "355631"&gt; &lt;node_name&gt;Port Sudan&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355632" ethyl_content_object_id="" geo_id = "355632"&gt; &lt;node_name&gt;Khartoum&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;node atlas_node_id = "355633" ethyl_content_object_id="3272" geo_id = "355633"&gt; &lt;node_name&gt;Swaziland&lt;/node_name&gt; &lt;/node&gt; &lt;/node&gt; &lt;/taxonomy&gt; </code></pre> <p></p> <p>Here what i tried:</p> <pre><code>@block = doc.xpath("taxonomies/taxonomy/node/node") @chld_name = @block.map do |node| node.children.map{|n| [n.name,n.text.strip] if n.elem? }.compact end.compact #p @chld_name def parse(element) children = element.children.reject{|e| e.is_a?(Nokogiri::XML::Text) &amp;&amp; e.text =~ /^\s*$/} if children.count == 1 &amp;&amp; children[0].is_a?(Nokogiri::XML::Text) children[0].text #p children else data = Hash.new children.each do |child| #p child data[child.name] = parse(child) end data end end campaigns = doc.xpath('/taxonomies/taxonomy/node/node').map{ |c| parse(c) } p campaigns </code></pre> <p>How can i do this? i tried many codes but no luck :(</p>