@@ -171,6 +171,50 @@ static std::string ConvertResultToCSV(MaterializedQueryResult &result) {
171
171
return csv_output;
172
172
}
173
173
174
+ static std::string EscapeXML (const std::string &in) {
175
+ std::string out;
176
+ out.reserve (in.size ());
177
+ for (char c : in) {
178
+ switch (c) {
179
+ case ' &' : out += " &" ; break ;
180
+ case ' <' : out += " <" ; break ;
181
+ case ' >' : out += " >" ; break ;
182
+ case ' "' : out += " "" ; break ;
183
+ case ' \' ' : out += " '" ; break ;
184
+ default : out += c; break ;
185
+ }
186
+ }
187
+ return out;
188
+ }
189
+
190
+ static std::string ConvertResultToXML (MaterializedQueryResult &result) {
191
+ std::string xml;
192
+ xml += " <results>\n " ;
193
+
194
+ for (idx_t row = 0 ; row < result.RowCount (); ++row) {
195
+ xml += " <row>\n " ;
196
+
197
+ for (idx_t col = 0 ; col < result.ColumnCount (); ++col) {
198
+ const std::string &col_name = result.ColumnName (col); // keep original
199
+ Value val = result.GetValue (col, row);
200
+
201
+ xml += " <column name=\" " ;
202
+ xml += EscapeXML (col_name);
203
+ xml += " \" >" ;
204
+
205
+ if (!val.IsNull ()) {
206
+ xml += EscapeXML (val.ToString ());
207
+ }
208
+
209
+ xml += " </column>\n " ;
210
+ }
211
+ xml += " </row>\n " ;
212
+ }
213
+
214
+ xml += " </results>\n " ;
215
+ return xml;
216
+ }
217
+
174
218
// Handle both GET and POST requests
175
219
void HandleHttpRequest (const duckdb_httplib_openssl::Request& req, duckdb_httplib_openssl::Response& res) {
176
220
std::string query;
@@ -261,6 +305,10 @@ void HandleHttpRequest(const duckdb_httplib_openssl::Request& req, duckdb_httpli
261
305
std::string csv_output = ConvertResultToCSV (*result);
262
306
res.set_header (" Content-Type" , " text/csv" );
263
307
res.set_content (csv_output, " text/csv" );
308
+ } else if (format == " XML" ) {
309
+ std::string xml_output = ConvertResultToXML (*result);
310
+ res.set_header (" Content-Type" , " application/xml" );
311
+ res.set_content (xml_output, " application/xml" );
264
312
} else {
265
313
// Default to NDJSON for DuckDB's own queries
266
314
std::string json_output = ConvertResultToNDJSON (*result);
0 commit comments