Method: MultiXml.parse
- Defined in:
- lib/multi_xml.rb
permalink .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
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, = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity xml ||= "" = DEFAULT_OPTIONS.merge() 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, [:disallowed_types]) if [:typecast_xml_value] rescue DisallowedTypeError raise rescue parser.parse_error => e raise(ParseError, e., e.backtrace) end hash = symbolize_keys(hash) if [:symbolize_keys] hash end |