Module: DbAgile::IO::XML

Defined in:
lib/dbagile/io/xml.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ :table_element_name  => "table",
:row_element_name    => "record"}

Class Method Summary collapse

Class Method Details

.to_xml(data, columns = nil, buffer = "", options = {}) ⇒ ...

Outputs some data as a XML string

Returns:

  • (...)

    the buffer itself



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dbagile/io/xml.rb', line 13

def to_xml(data, columns = nil, buffer = "", options = {})
  require 'builder'
  options = DEFAULT_OPTIONS.merge(options)
  ten, ren, cen = options[:table_element_name], 
                  options[:row_element_name],
                  options[:column_element_name]
  buffer << '<?xml version="1.0" encoding="UTF-8"?>' << "\n"
  buffer << "<#{ten}>"<< "\n"
  data.each{|row|
    buffer << "  " << "<#{ren}>" << "\n"
    columns.each{|column|
      buffer << "    " << "<#{column}>#{row[column].to_s.to_xs}</#{column}>" << "\n"
    }
    buffer << "  " << "</#{ren}>" << "\n"
  }
  buffer << "</#{ten}>"<< "\n"
  buffer
end