[tpm] Good examples?

Adam Prime adam.prime at utoronto.ca
Fri Nov 8 20:25:57 PST 2013


I have no idea if this is a good example or not, but i played around
with SAX a bit when i was looking at the nextbus api earlier this year. 
This is one of the screwy parser things i built in my experiments.

---

package AgencyParser;
use strict;

use base qw(XML::SAX::Base);

my $agencies;

sub start_element {
    my ($self, $el) = @_;

    if ($el->{'LocalName'} eq 'agency') {
        my $agency;

        foreach my $att (keys %{$el->{Attributes}}) {
            $agency->{$el->{'Attributes'}->{$att}->{LocalName}} =
                $el->{'Attributes'}->{$att}->{Value};
        }
        push @$agencies, $agency;
    }
}

sub end_document {
    $agencies = [];
}

sub end_document {
    my ($self, $doc) = @_;
    return $agencies;
}

1;

-----------

my $agency_parser = XML::SAX::ParserFactory->parser(
    Handler => AgencyParser->new()
);

my $agencies =
$agency_parser->parse_uri('http://webservices.nextbus.com/service/publicXMLFeed?command=agencyList');





More information about the toronto-pm mailing list