SAX: I can't STAND it (global variables)
by Arvinporthog other posts by this author
Jul 21 2006 11:22AM messages near this date
view in the new Beta List Site
Re: Install XML::Parser still failing
|
Re: SAX: I can't STAND it (global variables)
& XSLT Man, I'm hating the whole SAX interface. I do not understand
<br> the appeal. Things seem set up to make chained filters and
<br> weird things like that easier but the drawback is everything
<br> is hugely more complicated. Make simple things simple my ass.
<br> Even the littlest things are big enormous productions. Namespaces,
<br> global variables, callbacks--is this convenient for ANYONE?
<br> I've programmed SGML for over a decade so I understand the
<br> concepts and the event-driven model.
<br>
<br> OK, I exaggerate. I know SAX is probably very cool and I
<br> just need to learn best practices and probably a bit more
<br> about perl programming in general. But it's hard to be generous
<br> whilst pulling your hair out. I post for you all a simple
<br> example program and ask how it can be done without using
<br> global variables and, optimally, namespaces. It takes a list
<br> of xml files and checks for the presence of a required tag.
<br> When the program is done it prints a list of the files and
<br> whether or not they contained that tag.
<br>
<br> Arvin
<br>
<br> use XML::SAX;
<br> use XML::SAX::ExpatXS;
<br> use XML::Filter::BufferText;
<br> use strict 'vars';
<br> use vars qw($CurrentFile %Validated);
<br>
<br> my @xmlfiles = ('file1.xml', 'file2.xml', 'file3.xml');
<br>
<br> my $handler = new MySAXHandler;
<br> my $filter = new XML::Filter::BufferText (Handler => $handler);
<br> foreach my $file (@xmlfiles) {
<br> $CurrentFile = $file;
<br> $Validated{$file} = 'does not have title';
<br> my $parser = XML::SAX::ParserFactory->parser(Handler => $filter);
<br> $parser->parse_uri($file);
<br> }
<br> foreach my $file (keys %Validated) {
<br> print "$file: $Validated{$file}\n";
<br> }
<br>
<br>
<br> package MySAXHandler;
<br> use base qw(XML::SAX::Base);
<br>
<br> sub start_element {
<br> my ($self, $e) = @_;
<br> my $tag = $e->{LocalName};
<br> if ($tag eq 'title') {
<br> $main::Validated{$main::CurrentFile} = 'has title';
<br> }
<br> }
<br>
<br>
<br>
<br>
Thread:
Arvinporthog
Petr Cimprich
Petr Cimprich
Martin Owens
Mark - BLS CTR Thomas
|