Open a file handle on a string:
open my $output, '>', \$outputStr;
my $writer = new XML::Writer(OUTPUT => $output);
$writer->startTag("greeting", "class" => "simple");
<greeting class="simple">Hello, world!</greeting>
Having captured the output to a string you can print where ever you want and as many times as you want.
Update helps to copy and paste the right stuff!
True laziness is hard work