| Module | DBI::Utils::XMLFormatter |
| In: |
lib/dbi/utils/xmlformatter.rb
|
Formats results in XML.
good lord, what a mess.
nil in cols_as_tag, means "all columns expect those listed in cols_in_row_tag" add_row_tag_attrs are additional attributes which are inserted into the row-tag
# File lib/dbi/utils/xmlformatter.rb, line 24
24: def self.extended_row(dbrow, rowtag="row", cols_in_row_tag=[], cols_as_tag=nil, add_row_tag_attrs={}, output=STDOUT)
25: if cols_as_tag.nil?
26: cols_as_tag = dbrow.column_names - cols_in_row_tag
27: end
28:
29: output << "<#{rowtag}"
30: add_row_tag_attrs.each do |key, val|
31: # TODO: use textconv ? " substitution?
32: output << %{ #{key}="#{textconv(val)}"}
33: end
34: cols_in_row_tag.each do |key|
35: # TODO: use textconv ? " substitution?
36: output << %{ #{key}="#{dbrow[key]}"}
37: end
38: output << ">\n"
39:
40: cols_as_tag.each do |key|
41: output << " <#{key}>" + textconv(dbrow[key]) + "</#{key}>\n"
42: end
43: output << "</#{rowtag}>\n"
44: end
Generate XML for a row. The column names will surround the the values as tags.
# File lib/dbi/utils/xmlformatter.rb, line 11
11: def self.row(dbrow, rowtag="row", output=STDOUT)
12: #XMLFormatter.extended_row(dbrow, "row", [],
13: output << "<#{rowtag}>\n"
14: dbrow.each_with_name do |val, name|
15: output << " <#{name}>" + textconv(val) + "</#{name}>\n"
16: end
17: output << "</#{rowtag}>\n"
18: end
generate a full XML representation of the table.
Arguments and output are similar to row, with the exception of roottag, which is a container for the individual row tags.
# File lib/dbi/utils/xmlformatter.rb, line 51
51: def self.table(rows, roottag = "rows", rowtag = "row", output=STDOUT)
52: output << '<?xml version="1.0" encoding="UTF-8" ?>'
53: output << "\n<#{roottag}>\n"
54: rows.each do |row|
55: row(row, rowtag, output)
56: end
57: output << "</#{roottag}>\n"
58: end