Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

XML::Twig basic handlers

by jccunning (Acolyte)
on Aug 02, 2012 at 22:32 UTC ( #985105=perlquestion: print w/replies, xml ) Need Help??
jccunning has asked for the wisdom of the Perl Monks concerning the following question:

How do you write basic start_element, character, end_element handler events in XML::Twig similar to how it is done in XML::Parser as shown here:
#!/bin/perl use strict; use warnings; use XML::Parser; my %elements; my $parser = XML::Parser->new( Handlers => { Start=>\&handle_start, End=>\&handle_end, Char=>\&char_handler }); # specify xml file on command line foreach my $file ( @ARGV ) { eval { $parser->parsefile( $file ) }; die "can't parse file: $@" if $@; } sub handle_start { my( $expat, $element, %attrs ) = @_; if( %attrs ) { print "\t$element attributes:\n"; while( my( $key, $value ) = each( %attrs )) { print "\t\t$key => $value\n"; } } } sub char_handler { my ($p, $data) = @_; my $inf = $elements{$p->current_element}; if ($data =~ /\S/) {print "\t$data\n";} } sub handle_end { my( $expat, $element ) = @_; # print "$element\n"; }

Replies are listed 'Best First'.
Re: XML::Twig basic handlers
by Anonymous Monk on Aug 02, 2012 at 23:53 UTC
Re: XML::Twig basic handlers
by Anonymous Monk on Aug 03, 2012 at 15:21 UTC
    It might well be said that Twig is expressly designed to work at a higher level of abstraction ... and it is a level of abstraction that is very often useful and appropriate. I heartily recommend that you take a very close look at Twig, and that you try to get inside of its' designer's head, so to speak. You really do have to understand how Twig approaches its task, and funnel your project in that same direction. But, if you do that, Twig is a power-tool extraordinaire.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985105]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-06-23 16:14 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.