Method: MultiXml.parse

Defined in:
lib/multi_xml.rb

.parse(xml, options = {}) ⇒ Object

Parse an XML string or IO into Ruby.

Options

:symbolize_keys :: If true, will use symbols instead of strings for the keys.

:disallowed_types :: Types to disallow from being typecasted. Defaults to ['yaml', 'symbol']. Use [] to allow all types.

:typecast_xml_value :: If true, won't typecast values for parsed document

[View source]

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/multi_xml.rb', line 134

def parse(xml, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  xml ||= ""

  options = DEFAULT_OPTIONS.merge(options)

  xml = xml.strip if xml.respond_to?(:strip)
  begin
    xml = StringIO.new(xml) unless xml.respond_to?(:read)

    char = xml.getc
    return {} if char.nil?

    xml.ungetc(char)

    hash = undasherize_keys(parser.parse(xml) || {})
    hash = typecast_xml_value(hash, options[:disallowed_types]) if options[:typecast_xml_value]
  rescue DisallowedTypeError
    raise
  rescue parser.parse_error => e
    raise(ParseError, e.message, e.backtrace)
  end
  hash = symbolize_keys(hash) if options[:symbolize_keys]
  hash
end