private Method

XmlSimple.collapse(element)

Actually converts an XML document element into a data structure.

element:The document element to be collapsed.

Source Code

# File active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 461
def collapse(element)
  result = @options['noattr'] ? {} : get_attributes(element)

  if @options['normalisespace'] == 2
    result.each { |k, v| result[k] = normalise_space(v) }
  end

  if element.has_elements?
    element.each_element { |child|
      value = collapse(child)
      if empty(value) && (element.attributes.empty? || @options['noattr'])
        next if @options.has_key?('suppressempty') && @options['suppressempty'] == true
      end
      result = merge(result, child.name, value)
    }
    if has_mixed_content?(element)
      # normalisespace?
      content = element.texts.map { |x| x.to_s }
      content = content[0] if content.size == 1
      result[@options['contentkey']] = content
    end
  elsif element.has_text? # i.e. it has only text.
    return collapse_text_node(result, element)
  end

  # Turn Arrays into Hashes if key fields present.
  count = fold_arrays(result)

  # Disintermediate grouped tags.
  if @options.has_key?('grouptags')
    result.each { |key, value|
      next unless (value.instance_of?(Hash) && (value.size == 1))
      child_key, child_value = value.to_a[0]
      if @options['grouptags'][key] == child_key
        result[key] = child_value
      end
    }
  end

  # Fold Hases containing a single anonymous Array up into just the Array.
  if count == 1 
    anonymoustag = @options['anonymoustag']
    if result.has_key?(anonymoustag) && result[anonymoustag].instance_of?(Array)
      return result[anonymoustag]
    end
  end

  if result.empty? && @options.has_key?('suppressempty')
    return @options['suppressempty'] == '' ? '' : nil
  end

  result
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.