# (DBI::Utils::XMLFormatter) Output results as a string?

**URL:** <https://www.ruby-forum.com/t/dbi-xmlformatter-output-results-as-a-string/60997>\
**Category:** Ruby\
**Created:** [May 19, 2006, 4:21am UTC](https://www.ruby-forum.com/t/dbi-xmlformatter-output-results-as-a-string/60997 "2006-05-19T04:21:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [May 19, 2006, 4:21am UTC](https://www.ruby-forum.com/t/dbi-xmlformatter-output-results-as-a-string/60997/1 "2006-05-19T04:21:25Z")

</div>

[xml.rb]  
require ‘dbi’  
DBI::Utils::XMLFormatter.table(dbh.select\_all(“SELECT \* FROM people”))  
[/xml.rb]

When I run this from the dos prompt, I do get the appropriate output,  
IE:  
c:\scripts\>ruby xml.rb

 \<?xml version="1.0" encoding="UTF-8" ?\> 1 Wanda 62.5 2 Robert 75.0 c:\scripts\>

But from within the script, I’m unable to capture this output as a  
string, for example:  
irb\> require ‘dbi’  
irb\> asdf = DBI::Utils::XMLFormatter.table(dbh.select\_all(“SELECT \*  
FROM people”))  
irb\> puts asdf  
=\> #IO:0x2c18968  
[/xml\_borked.rb]

I’d like to be able to use puts to output the results of the  
DBI::Utils::XMLFormatter method.

Any suggestions?

Thank you

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [May 19, 2006, 5:13am UTC](https://www.ruby-forum.com/t/dbi-xmlformatter-output-results-as-a-string/60997/2 "2006-05-19T05:13:49Z")

</div>

x1 wrote:

> ```
> <id>1</id>
> 
> ```
> 
> But from within the script, I’m unable to capture this output as a
> 
> Any suggestions?

The XMLFormatter.table method takes four arguments. The fourth argument  
specifies an IO handle to send the output. By default this is set to  
STDOUT. So, your options are to either provide an IO object for the  
fourth argument, or temporarily redirect STDOUT.

I should mention that this part of the dbi package has not been  
refactored or properly documented via rdoc yet. We need to get on that.  
🙂

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [May 29, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/dbi-xmlformatter-output-results-as-a-string/60997/3 "2006-05-29T23:40:49Z")

</div>

I never could get it to work properly… It’s like… when I used the  
fourth variable as you suggested, the component was missing.

IE:

irb\> DBI::Utils::XMLFormatter.table(dbh.select\_all(“SELECT \*  
FROM people”), nil, nil, asdf)

puts asdf

\<\>  
  
  
  
  
\</\>

When I threw in the variable as the second paramater, I would get the  
 piece but not the … Hard to expain right now because I  
have no db to test it on but at work, this is what happened… Does  
this provide enough detail?
