Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

XML::Twig output to filehandle

by toddgow (Initiate)
on Feb 12, 2009 at 21:20 UTC ( [id://743462]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to get my hands around XML::Twig(like learning a whole new application) and I am having problems getting it to print to a filehandle. I only want to print the data that I have pulled not the whole document and eventually I want this to be a different HTML file. Here is a sample of what I am trying to do:
My XML File: <script type="ApplicationPerspective" version="5.3.13.179" recorder="8 +.6.59.276" sav="25" guid="296A95D0-E8B6-4989-AA21-126796A3AD3F" xmlns +="http://www.keynote.com/namespaces/tstp/script"> <name> <![CDATA[GT Amadeus]]> </name> <actions> <action FrameErrorFatal="1" MetaErrorFatal="1"> <name> <![CDATA[Home Page]]> </name> <description> <![CDATA[]]> </description> </action> </actions> </script> use strict; use warnings; use XML::Twig; my $file = $ARGV[0]; my $output = "/home/todd/Scripts/output.txt"; open( OUT, ">$output") or die "can't open file: $!"; my $twig = new XML::Twig( twig_handlers => { name => \&name } ); $twig->parsefile($file); sub name { my ($twig, $name) = @_; my $stuff = $name->text(); $stuff->print( \*OUT); # print "$stuff\n"; $twig->purge; } close(OUT);
What the is the best method to do this? In the XML::Twig docs it only has an example of using a filehandle with twig_print_outside_roots. Thanks in advance, Todd

Replies are listed 'Best First'.
Re: XML::Twig output to filehandle
by eff_i_g (Curate) on Feb 12, 2009 at 22:02 UTC
    I modified the input to consolidate the information, but the same concept applies:
    use warnings; use strict; use XML::Twig; undef $/; my $XML = <DATA>; open my $OUT, '>', '/tmp/twig_out' or die "Can't open file: $!"; my $twig = XML::Twig->new( twig_handlers => { name => sub { ### If you want the text. if (1) { print $OUT $_->text(), "\n"; } ### If you want the elements. else { $_->print($OUT); } } } ); $twig->parse($XML); close $OUT or die $!; __DATA__ <script type="ApplicationPerspective" version="5.3.13.179" recorder="8 +.6.59.276" sav="25" guid="296A95D0-E8B6-4989-AA21-126796A3AD3F" xmlns +="http://www.keynote.com/namespaces/tstp/script"> <name> <![CDATA[GT Amadeus]]> </name> <actions> <action FrameErrorFatal="1" MetaErrorFatal="1"> <name> <![CDATA[Home Page]]> </name> <description> <![CDATA[]]> </description> </action> </actions> </script>

      When modifying special variables, it's a good idea to localize them and shorten the scope.

      See perlvar

      Update:
      I was thinking of $/


      hth,
      PooLpi

      'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

        If you are talking about $_ in the handler, it _is_ localized (and set) by XML::Twig, so no worries there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://743462]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found