Nokogiri recursively get all children

后端 未结 2 1425
花落未央
花落未央 2021-02-07 23:19

The Problem

I am running some statistics against various URLS. I want to find the top level element with the most concentrated number of children. The method that I wo

相关标签:
2条回答
  • 2021-02-07 23:34
    # Non-recursive
    class Nokogiri::XML::Node
      def descendant_elements
        xpath('.//*')
      end
    end
    
    # Recursive 1
    class Nokogiri::XML::Node
      def descendant_elements
        element_children.map{ |kid|
          [kid, kid.descendant_elements]
        }.flatten
      end
    end
    
    # Recursive 2
    class Nokogiri::XML::Node
      def descendant_elements
        kids = element_children.to_a
        kids.concat(kids.map(&:descendant_elements)).flatten
      end
    end
    
    0 讨论(0)
  • 2021-02-07 23:48

    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 this
    result = []
    doc.traverse {|node| result << node }
    result
    
    # or, 
    require 'enumerator'
    result = doc.enum_for(:traverse).map
    
    0 讨论(0)
提交回复
热议问题