Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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"; }

Comment on XML::Twig basic handlers
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985105]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-10-01 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (21 votes), past polls