beamsack has asked for the wisdom of the Perl Monks concerning the following question:
Example of a story elementuse strict; use XML::Parser; use LWP::Simple; my @curr; my @stories; # global hash my %story; my $xml = get("http://www.slashdot.org/slashdot.xml"); die "Failed to obtain xml " unless defined($xml); my $p = XML::Parser->new(Style => 'Stream'); $p->parse($xml); # print out the results foreach my $hashref (@stories) { print "$hashref->{title}\n"; } # StartTag - called when the start of an XML tag is found sub StartTag { my($p, $tag) = @_; push @curr, $tag; } # EndTag - called when the end of a XML tag is seen sub EndTag { my($p, $tag) = @_; # pushes the hash ref onto the array if ($tag eq 'story') { push @stories, \%story; } pop @curr; } # Text - called when text data is encountered sub Text { unless ($curr[-1] eq 'story') { $story{$curr[-1]} = $_; } }
<story> <title>Debian And WineX</title> <url>http://slashdot.org/article.pl?sid=02/05/28/1515220</url> <time>2002-05-28 18:25:01</time> <author>Hemos</author> ... </story>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with a Ref to a global variable while using XML::Parser
by clintp (Curate) on May 28, 2002 at 23:50 UTC | |
by beamsack (Scribe) on May 29, 2002 at 03:39 UTC | |
|
(crazyinsomniac: perlref) Re: Problem with a Ref to a global variable while using XML::Parser
by crazyinsomniac (Prior) on May 29, 2002 at 00:24 UTC |