module ActiveSupport::XmlMini_REXML

def collapse(element, depth)

The document element to be collapsed.
element::

Actually converts an XML document element into a data structure.
def collapse(element, depth)
  hash = get_attributes(element)
  if element.has_elements?
    element.each_element { |child| merge_element!(hash, child, depth - 1) }
    merge_texts!(hash, element) unless empty_content?(element)
    hash
  else
    merge_texts!(hash, element)
  end
end

def empty_content?(element)

XML element to be checked.
element::

Determines if a document element has text content
def empty_content?(element)
  element.texts.join.blank?
end

def get_attributes(element)

XML element to extract attributes from.
element::

Returns an empty Hash if node has no attributes.
Converts the attributes array of an XML element into a hash.
def get_attributes(element)
  attributes = {}
  element.attributes.each { |n, v| attributes[n] = v }
  attributes
end

def merge!(hash, key, value)

Value to be associated with key.
value::
Key to be added.
key::
Hash to add key/value pair to.
hash::

appended to that Array.
an Array, it will be wrapped in an Array. Then the new value is
already exists and the existing value associated with key is not
Adds a new key/value pair to an existing Hash. If the key to be added
def merge!(hash, key, value)
  if hash.has_key?(key)
    if hash[key].instance_of?(Array)
      hash[key] << value
    else
      hash[key] = [hash[key], value]
    end
  elsif value.instance_of?(Array)
    hash[key] = [value]
  else
    hash[key] = value
  end
  hash
end

def merge_element!(hash, element, depth)

XML element to merge into hash
element::
Hash to merge the converted element into.
hash::

Convert an XML element and merge into the hash
def merge_element!(hash, element, depth)
  raise REXML::ParseException, "The document is too deep" if depth == 0
  merge!(hash, element.name, collapse(element, depth))
end

def merge_texts!(hash, element)

XML element whose texts are to me merged into the hash
element::
Hash to add the converted element to.
hash::

Merge all the texts of an element into the hash
def merge_texts!(hash, element)
  unless element.has_text?
    hash
  else
    # must use value to prevent double-escaping
    texts = +""
    element.texts.each { |t| texts << t.value }
    merge!(hash, CONTENT_KEY, texts)
  end
end

def parse(data)

XML Document string or IO to parse
data::

and uses the defaults from Active Support.
Same as XmlSimple::xml_in but doesn't shoot itself in the foot,

Parse an XML Document string or IO into a simple hash.
def parse(data)
  if !data.respond_to?(:read)
    data = StringIO.new(data || "")
  end
  if data.eof?
    {}
  else
    require_rexml unless defined?(REXML::Document)
    doc = REXML::Document.new(data)
    if doc.root
      merge_element!({}, doc.root, XmlMini.depth)
    else
      raise REXML::ParseException,
        "The document #{doc.to_s.inspect} does not have a valid root"
    end
  end
end

def require_rexml

def require_rexml
  silence_warnings { require "rexml/document" }
rescue LoadError => e
  $stderr.puts "You don't have rexml installed in your application. Please add it to your Gemfile and run bundle install"
  raise e
end