mcoblentz has asked for the wisdom of the Perl Monks concerning the following question:
Continuing on the ATOM parsing question, I have extracted a snipped of response from a CDATA section which contains more XML. Monk Jenda's very fine suggestion was to re-parse. However, I am getting a:
Undefined subroutine &main::read_chunk_of_data called at quake_parsing +_9 line 81.
response from the system. I copied the 'example' straight out of CPAN but it seems that the call I'm making is not valid. The entire program is included below and the issue seems to occur around line 82 (now marked with a bunch of asterisks).
#!/usr/bin/perl -w use XML::FeedPP; use XML::LibXML; use XML::Rules; use LWP::Simple; use Time::Piece; use strict; use warnings; # ----------------------------------------------- # open the quake marker file for writing # ----------------------------------------------- open (quake_marker, '>/Users/coblem/xplanet/markers/quake.txt') or die + $!; # ----------------------------------------------- # get the Atom feed from the USGS # ----------------------------------------------- #my $source = '/Users/coblem/xplanet/quake/quake.xml'; #use this line +only for local testing my $source = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary +/4.5_day.atom'; #use this line for feed testing my $feed = XML::FeedPP->new( $source ); # get the date and time. While not needed right now, this will help l +ater with managing expired # data. # ----------------------------------------------- # get date and time, day of the year # ----------------------------------------------- my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year += 1900; my $cur_time = "$wday $mday $mon $year, $hour:$min:$sec"; # ----------------------------------------------- # update the output file header # ----------------------------------------------- my $dayofYear = localtime[7]; print (quake_marker "# This Quake file created by quake_parsing_9", "\ +n"); print (quake_marker "# Matt Coblentz; Perl version unknown", "\n"); print (quake_marker "# For more information, see the USGS website", "\ +n"); print (quake_marker "# Last Updated: ", $cur_time, "\n", "\# \n"); # Incorporating a rules section so that we can extract the information + from the CDATA section # and any other specific fields we might need. First attempt will be +to pull the 'Updated' # field data from the Atom feed. I tried to follow the example in the + CPAN summary but I'm # clearly lost. # PerlMonk Jenda pointed out that I should use the standard rules prev +iously defined. There were # other helpful comments about the author and update rules as well, wh +ich are now deprecated. my @rules = ( _default => 'content', dd => 'content trim', ); my $parser = XML::Rules->new(rules => \@rules); # Need to convert the feed object to a string. Thanks to PerlMonk Fro +zenwithjoy my $atom = $feed->to_string(); $parser->parse( $atom ); # This section extracts the title field, then performs string manipula +tions to extract the # long location data and the magnitude. Funny that USGS does not have + a magnitude field # in this feed. foreach my $quake( $feed->get_item() ) { my $title = $quake->title(); my $place = substr($title, 8); my $magnitude = substr($title, 2,3); # Thanks to PerlMonk Jakeease for fixing these extractions: my $id = $quake->get( "id"); my $update = $quake->get( "updated" ); my $location = $quake->get( "georss:point"); # *********************************************** # *********************************************** # THIS IS THE PROBLEM AREA! # *********************************************** # *********************************************** my $summary = $quake->get( "summary"); print $summary; while ($summary = read_chunk_of_data()) { $parser->parse_chunk($summary); } my $data = $parser->last_chunk(); my $dd = $data->get( "dd"); print $dd, "\n"; # print (quake_marker $location, "\"\" color=Yellow symbolsize=65", + "\n"); # print (quake_marker $location, "\"", $magnitude, "\" color=Yellow + align=Above", "\n"); # print (quake_marker "\n"); print (quake_marker "\n", "Magnitude ", $magnitude, " ", $place, " +\n"); print (quake_marker $location, " \"\" color=Yellow symbolsize=65", + "\n"); print (quake_marker $location, " \"", $magnitude, "\" color=Yellow + align=Above", "\n"); print "\n"; } close (quake_marker);
What am I doing wrong? I'm obviously invoking something that isn't there, but what should it be?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML parsing with XML::Rules
by Khen1950fx (Canon) on Jun 16, 2013 at 03:30 UTC | |
|
Re: XML parsing with XML::Rules
by kcott (Archbishop) on Jun 16, 2013 at 09:17 UTC | |
by Anonymous Monk on Jun 16, 2013 at 09:43 UTC | |
by kcott (Archbishop) on Jun 16, 2013 at 10:58 UTC | |
by Anonymous Monk on Jun 16, 2013 at 11:20 UTC | |
by kcott (Archbishop) on Jun 16, 2013 at 18:48 UTC | |
|
Re: XML parsing with XML::Rules
by poj (Abbot) on Jun 16, 2013 at 18:15 UTC | |
by poj (Abbot) on Jun 16, 2013 at 20:00 UTC | |
|
Re: XML parsing with XML::Rules
by jakeease (Friar) on Jun 17, 2013 at 09:12 UTC | |
by mcoblentz (Scribe) on Jun 18, 2013 at 00:02 UTC | |
by jakeease (Friar) on Jun 18, 2013 at 07:17 UTC |