Method: XmlSimple#xml_in

Defined in:
lib/xmlsimple.rb

#xml_in(string = nil, options = nil) ⇒ Object

Converts an XML document in the same way as the Perl module XML::Simple.

string

XML source. Could be one of the following:

  • nil: Tries to load and parse ‘<scriptname>.xml’.

  • filename: Tries to load and parse filename.

  • IO object: Reads from object until EOF is detected and parses result.

  • XML string: Parses string.

options

Options to be used.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/xmlsimple.rb', line 149

def xml_in(string = nil, options = nil)
  handle_options('in', options)

  # If no XML string or filename was supplied look for scriptname.xml.
  if string.nil?
    string = File::basename($0).dup
    string.sub!(/\.[^.]+$/, '')
    string += '.xml'

    directory = File::dirname($0)
    @options['searchpath'].unshift(directory) unless directory.nil?
  end

  if string.is_a?(String)
    if string =~ /<.*?>/m
      @doc = parse(string)
    elsif string == '-'
      @doc = parse($stdin.read)
    else
      filename = find_xml_file(string, @options['searchpath'])

      if @options.has_key?('cache')
        @options['cache'].each { |scheme|
          case(scheme)
          when 'storable'
            content = @@cache.restore_storable(filename)
          when 'mem_share'
            content = @@cache.restore_mem_share(filename)
          when 'mem_copy'
            content = @@cache.restore_mem_copy(filename)
          else
            raise ArgumentError, "Unsupported caching scheme: <#{scheme}>."
          end
          return content if content
        }
      end

      @doc = load_xml_file(filename)
    end
  elsif string.respond_to?(:read)
    @doc = parse(string.read)
  else
    raise ArgumentError, "Could not parse object of type: <#{string.class}>."
  end

  result = collapse(@doc.root)
  result = @options['keeproot'] ? merge({}, @doc.root.name, result) : result
  put_into_cache(result, filename)
  result
end