Re: SAX: I can't STAND it (global variables)
by Petr Cimprich other posts by this author
Jul 24 2006 12:18AM messages near this date
view in the new Beta List Site
SAX: I can't STAND it (global variables)
|
Re: SAX: I can't STAND it (global variables)
& XSLT SAX is low-level but I still find it convenient for a task like this.
These are few points which can make your script simpler and faster:
- object properties instead of global vars (as you noted later)
- XML::Filter::BufferText is unnecessary with ExpatXS
- no need for two loops
A modified version is attached.
Petr
arvinporthog wrote:
> Man, I'm hating the whole SAX interface. I do not understand
> the appeal. Things seem set up to make chained filters and
> weird things like that easier but the drawback is everything
> is hugely more complicated. Make simple things simple my ass.
> Even the littlest things are big enormous productions. Namespaces,
> global variables, callbacks--is this convenient for ANYONE?
> I've programmed SGML for over a decade so I understand the
> concepts and the event-driven model.
>
> OK, I exaggerate. I know SAX is probably very cool and I
> just need to learn best practices and probably a bit more
> about perl programming in general. But it's hard to be generous
> whilst pulling your hair out. I post for you all a simple
> example program and ask how it can be done without using
> global variables and, optimally, namespaces. It takes a list
> of xml files and checks for the presence of a required tag.
> When the program is done it prints a list of the files and
> whether or not they contained that tag.
>
> Arvin
>
> use XML::SAX;
> use XML::SAX::ExpatXS;
> use XML::Filter::BufferText;
> use strict 'vars';
> use vars qw($CurrentFile %Validated);
>
> my @xmlfiles = ('file1.xml', 'file2.xml', 'file3.xml');
>
> my $handler = new MySAXHandler;
> my $filter = new XML::Filter::BufferText (Handler => $handler);
> foreach my $file (@xmlfiles) {
> $CurrentFile = $file;
> $Validated{$file} = 'does not have title';
> my $parser = XML::SAX::ParserFactory->parser(Handler => $filter);
> $parser->parse_uri($file);
> }
> foreach my $file (keys %Validated) {
> print "$file: $Validated{$file}\n";
> }
>
>
> package MySAXHandler;
> use base qw(XML::SAX::Base);
>
> sub start_element {
> my ($self, $e) = @_;
> my $tag = $e->{LocalName};
> if ($tag eq 'title') {
> $main::Validated{$main::CurrentFile} = 'has title';
> }
> }
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Perl-XML mailing list
> Perl-XML@[...].com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--
Petr Cimprich
Ginger Alliance
www.gingerall.com
Thread:
Arvinporthog
Petr Cimprich
Petr Cimprich
Martin Owens
Mark - BLS CTR Thomas
|