Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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
[choroba]: that's 2012
[zentara]: I just acquired a computer with the card type, and wondered if it was best to stick with the open-source code or use NVIDIA's proprietary code
[zentara]: do you feel good about a non-open-source code, being in control of your display functions?
[choroba]: If you're not going to play 3d games on it, using the open source driver should be enough
[zentara]: choroba that is true, and the wise choice, but I want to see how fast I can get fglrx3dspheres to run... just as an intellectual challenge :-)

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-05-26 14:00 GMT
Find Nodes?
    Voting Booth?