public Method

XmlSimple.xml_in(string = nil, options = nil)

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.</scriptname>

Source Code

# File active_support/vendor/xml-simple-1.0.11/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)
    string.sub!(/\.[^.]+$/, '')
    string += '.xml'

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

  if string.instance_of?(String)
    if string =~ /<.*?>/m
      @doc = parse(string)
    elsif string == '-'
      @doc = parse($stdin.readlines.to_s)
    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.kind_of?(IO) || string.kind_of?(StringIO)
    @doc = parse(string.readlines.to_s)
  else
    raise ArgumentError, "Could not parse object of type: <#{string.type}>."
  end

  result = collapse(@doc.root)
  result = @options['keeproot'] ? merge({}, @doc.root.name, result) : result
  put_into_cache(result, filename)
  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.