Nokogiri recursively get all children
the traverse method yields the current node and all children to a block, recursively.
# if you would like it to be returned as an array, rather than each node being yielded to a block, you can do thisresult = []doc.traverse {|node| result << node }result# or, require 'enumerator'result = doc.enum_for(:traverse).map
# Non-recursiveclass Nokogiri::XML::Node def descendant_elements xpath('.//*') endend# Recursive 1class Nokogiri::XML::Node def descendant_elements element_children.map{ |kid| [kid, kid.descendant_elements] }.flatten endend# Recursive 2class Nokogiri::XML::Node def descendant_elements kids = element_children.to_a kids.concat(kids.map(&:descendant_elements)).flatten endend