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

Re: XML::SAX parameters

by choroba (Cardinal)
on Oct 02, 2015 at 12:59 UTC ( [id://1143657]=note: print w/replies, xml ) Need Help??


in reply to XML::SAX parameters

I used to be one of the teachers of this course.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use XML::SAX; use XML::SAX::Writer; my $out; my $writer = XML::SAX::Writer->new(Output => \$out); my $filter = Element_Attribute_Counter->new(Handler => $writer); my $parser = XML::SAX::ParserFactory->parser(Handler => $filter); $parser->parse_uri($ARGV[0]); print "$out\n", Dumper($filter->get_count); package Element_Attribute_Counter; use base qw( XML::SAX::Base ); use Scalar::Util qw( refaddr ); my %count; sub start_element { my ($self, $element) = @_; my $addr = refaddr $self; $count{$addr}{ $element->{Name} }++; my $attributes = $element->{Attributes}; for my $attribute (keys %$attributes) { $count{$addr}{ "@" . $attributes->{$attribute}{Name} }++; } $element->{Name} = uc $element->{Name}; $self->SUPER::start_element($element); } sub get_count { my $self = shift; return $count{refaddr $self}; }

Update: As usually with callbacks, the easiest way to communicate is through closures. This example uses a lexical package-scoped variable. Note that each object has a different slot in the hash, so you can use the same class for several objects (the count is "inside-out").

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: XML::SAX parameters
by Artimus (Sexton) on Oct 05, 2015 at 10:05 UTC

    While it all was in one file with handler package - all worked.

    After I checked it and splitted it in different modules at different places of the project and mentioned handler as needed. Parsing was working, but method for the only returning results didn't work.

    Well the solution was to put line return \@results; right to the

    sub end_document{ $self = shift; return \@results;}
    method :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found