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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E2b3bbf241a5daee3ee7696a7d4b2015?d=identicon&s=25 Vrtwo Lastname (d0t1q)
on 2006-05-19 04:21
(Received via mailing list)
[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" ?>
   <rows>
   <row>
     <id>1</id>
     <name>Wanda</name>
     <height>62.5</height>
   </row>
   <row>
     <id>2</id>
     <name>Robert</name>
     <height>75.0</height>
   </row>
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
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2006-05-19 05:13
(Received via mailing list)
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
E2b3bbf241a5daee3ee7696a7d4b2015?d=identicon&s=25 Vrtwo Lastname (d0t1q)
on 2006-05-29 23:40
(Received via mailing list)
I never could get it to work properly... It's like.. when I used the
fourth variable as you suggested, the <rows> component was missing.

IE:

irb> DBI::Utils::XMLFormatter.table(dbh.select_all("SELECT *
FROM people"), nil, nil, asdf)

puts asdf

<>
<row>
   <yada>
   </yada>
</row>
</>

When I threw in the variable as the second paramater, I would get the
<rows> piece but not the <row>.... 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?
This topic is locked and can not be replied to.