Module: MultiXML::Parsers::DomParser Private

Included in:
Libxml, Nokogiri, Oga
Defined in:
lib/multi_xml/parsers/dom_parser.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Shared DOM traversal logic for converting XML nodes to hashes

Used by Nokogiri, LibXML, and Oga parsers. Including modules must implement:

  • each_child(node) { |child| ... }
  • each_element_attr(node) { |attr| ... } (non-namespace-decl attrs only)
  • each_namespace_decl(node) { |prefix_or_nil, uri| ... }
  • element_parts(node) -> [prefix_or_nil, local_name]
  • attr_parts(attr) -> [prefix_or_nil, local_name]

Instance Method Summary collapse

Instance Method Details

#node_to_hash(node, hash = {}, mode: :strip) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert an XML node to a hash representation

Parameters:

  • node (Object)

    XML node to convert

  • hash (Hash) (defaults to: {})

    Accumulator hash for results

  • mode (Symbol) (defaults to: :strip)

    Namespace handling mode (:strip, :preserve)

Returns:

  • (Hash)

    Hash representation of the node



29
30
31
32
33
34
35
36
37
# File 'lib/multi_xml/parsers/dom_parser.rb', line 29

def node_to_hash(node, hash = {}, mode: :strip)
  node_hash = {TEXT_CONTENT_KEY => +""}
  add_value(hash, format_element_name(node, mode), node_hash)
  collect_children(node, node_hash, mode)
  collect_namespace_decls(node, node_hash, mode)
  collect_attributes(node, node_hash, mode)
  strip_whitespace_content(node_hash)
  hash
end