Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello again,

Well I finally got XML::Twig working on my windows box, and have started the learning process. So far everything has made sense, but could someone explain how I can dump my XML code to a file after?

I'm using the following code from some examples I found online with several modifications to suit my needs

#!/perl/bin/perl -w use strict; use XML::Twig; my $field= 'last_ds'; my $twig= new XML::Twig; $twig->parsefile( "RoamAboutAP_192.168.25.88.xml"); my $root= $twig->root; my @ds= $root->children; my @sorted= sort { $b->first_child( $field)->text <=> $a->first_child( $field)->text } @ds; open (XML, ">>test.xml") || warn; foreach my $attr (@ds) { my $g = $attr->first_child( 'min')->text; my $blk= $attr->first_child( 'max')->text; my $blg= sprintf( "TEST"); my $eblg= new XML::Twig::Elt( 'Average', $blg); $eblg->paste( 'last_child', $attr); } $twig->set_pretty_print('indented'); $twig->print; close XML;
Thanks again!

Replies are listed 'Best First'.
Re: How do I print XML to a file with Twig?
by mirod (Canon) on Jan 20, 2003 at 19:14 UTC

    Very easy: pass a filehandle to print:

    open( XML_OUT, ">output.xml") or die $!; $twig->print( \*XML_OUT);
      Thanks Mirod!,

      I wasn't sure of the syntax.
      Thanks Again