ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
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

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved