Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Ouput XML::Writer to scalar

by jeffa (Bishop)
on May 02, 2003 at 14:05 UTC ( #254998=sourcecode: print w/replies, xml ) Need Help??
Category: Text Processing
Author/Contact Info /msg jeffa
Description: From time to time i have found myself wanting to capture the output from XML::Writer to a scalar instead of a file handle. It is simple enough, but i thought i would share here. Since XML::Writer expects an IO::Handle module for it's OUTPUT parameter, pass it an IO::Scalar. At the end, we can get a hold of that handle via XML::Writer::getOutput().
use strict;
use warnings;

use IO::Scalar;
use XML::Writer;

my $writer = XML::Writer->new(
    OUTPUT => IO::Scalar->new()
);

$writer->startTag("greeting", "class" => "simple");
$writer->characters("Hello, world!");
$writer->endTag("greeting");
$writer->end();

my $output = $writer->getOutput();

# optionally run $output through a filter

print $output;
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://254998]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2020-06-01 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (11 votes). Check out past polls.

    Notices?