public Method

XmlSimple.xml_out(ref, options = nil)

Converts a data structure into an XML document.

ref:Reference to data structure to be converted into XML.
options:Options to be used.

Source Code

# File active_support/vendor/xml-simple-1.0.11/xmlsimple.rb, line 212
def xml_out(ref, options = nil)
  handle_options('out', options)
  if ref.instance_of?(Array)
    ref = { @options['anonymoustag'] => ref }
  end

  if @options['keeproot']
    keys = ref.keys
    if keys.size == 1
      ref = ref[keys[0]]
      @options['rootname'] = keys[0]
    end
  elsif @options['rootname'] == ''
    if ref.instance_of?(Hash)
      refsave = ref
      ref = {}
      refsave.each { |key, value|
        if !scalar(value)
          ref[key] = value
        else
          ref[key] = [ value.to_s ]
        end
      }
    end
  end

  @ancestors = []
  xml = value_to_xml(ref, @options['rootname'], '')
  @ancestors = nil

  if @options['xmldeclaration']
    xml = @options['xmldeclaration'] + "\n" + xml
  end

  if @options.has_key?('outputfile')
    if @options['outputfile'].kind_of?(IO)
      return @options['outputfile'].write(xml)
    else
      File.open(@options['outputfile'], "w") { |file| file.write(xml) }
    end
  end
  xml
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.