def makeSVFile( filename, fieldSeparator = ",", dbid = @dbid, query = nil, qid = nil, qname = nil )
      File.open( filename, "w" ) { |file|

         if dbid
            doQuery( dbid, query, qid, qname )
         end

         if @records and @fields

            # ------------- write field names on first line ----------------
            output = ""
            fieldNamesBlock = proc { |element|
               if element.is_a?(REXML::Element) and element.name == "label" and element.has_text?
                  output << "#{element.text}#{fieldSeparator}"
               end
            }
            processChildElements( @fields, true, fieldNamesBlock )

            output << "\n"
            output.sub!( "#{fieldSeparator}\n", "\n" )
            file.write( output )

            # ------------- write records ----------------
            output = ""
            valuesBlock = proc { |element|
               if element.is_a?(REXML::Element)
                  if element.name == "record"
                     if output.length > 1
                        output << "\n"
                        output.sub!( "#{fieldSeparator}\n", "\n" )
                        file.write( output )
                     end
                     output = ""
                  elsif  element.name == "f"
                     if  element.has_text?
                        text = element.text
                        text.gsub!("<BR/>","\n")
                        text = "\"#{text}\"" if text.include?( fieldSeparator )
                        output << "#{text}#{fieldSeparator}"
                     else
                        output << "#{fieldSeparator}"
                     end
                  end
               end
            }

            processChildElements( @records, false, valuesBlock )
            if output.length > 1
              output << "\n"
              output.sub!( "#{fieldSeparator}\n", "\n" )
              file.write( output )
              output = ""
            end
         end
      }
   end
def makeSVFile( filename, fieldSeparator = ",", dbid = @dbid, query = nil, qid = nil, qname = nil )
      File.open( filename, "w" ) { |file|

         if dbid
            doQuery( dbid, query, qid, qname )
         end

         if @records and @fields

            # ------------- write field names on first line ----------------
            output = ""
            fieldNamesBlock = proc { |element|
               if element.is_a?(REXML::Element) and element.name == "label" and element.has_text?
                  output << "#{element.text}#{fieldSeparator}"
               end
            }
            processChildElements( @fields, true, fieldNamesBlock )

            output << "\n"
            output.sub!( "#{fieldSeparator}\n", "\n" )
            file.write( output )

            # ------------- write records ----------------
            output = ""
            valuesBlock = proc { |element|
               if element.is_a?(REXML::Element)
                  if element.name == "record"
                     if output.length > 1
                        output << "\n"
                        output.sub!( "#{fieldSeparator}\n", "\n" )
                        file.write( output )
                     end
                     output = ""
                  elsif  element.name == "f"
                     if  element.has_text?
                        text = element.text
                        text.gsub!("<BR/>","\n")
                        text = "\"#{text}\"" if text.include?( fieldSeparator )
                        output << "#{text}#{fieldSeparator}"
                     else
                        output << "#{fieldSeparator}"
                     end
                  end
               end
            }

            processChildElements( @records, false, valuesBlock )
            if output.length > 1
              output << "\n"
              output.sub!( "#{fieldSeparator}\n", "\n" )
              file.write( output )
              output = ""
            end
         end
      }
   end
Make a CSV file using the results of a query.
 Specify a different separator in the second paramater.
 Fields containing the separator will be double-quoted.

 e.g. makeSVFile( "contacts.txt", "\t", nil )
 e.g. makeSVFile( "contacts.txt", ",", "dhnju5y7", nil, nil, "List Changes" )