mcoblentz has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to parse an ATOM feed and only want a few of the data in certain fields (updated, georss:point, which = the location in +/- format, and the time of the occurrence, which is in a CDATA field).
I was thinking to use XML::Rules to parse out the georss:point field and from there decide how to extract and parse the CDATA Time stamp but I can't get past the initial run - I get a:
"not well-formed (invalid token) at line 1, column 25, byte 25 at quake_parsing_4a1.pl line 22" response when I add in the rules and try to parse the xml. What am I doing wrong?
I welcome suggestions on how to debug this. Thanks for your suggestions!
A snippet of the feed:
<entry> <id>urn:earthquake-usgs-gov:us:c000hkbe</id> <title>M 4.9 - 71km SW of Paita, Peru</title> <updated>2013-06-10T16:39:37.373Z</updated> <link rel="alternate" type="text/html" href="http://earthquake.usgs.go +v/earthquakes/eventpage/usc000hkbe"/> <link rel="alternate" type="application/cap+xml" href="http://earthqua +ke.usgs.gov/earthquakes/eventpage/usc000hkbe.cap"/> <summary type="html"> <![CDATA[ <p class="quicksummary"><a href="http://earthquake.usgs.gov/earthquake +s/eventpage/usc000hkbe#dyfi" class="mmi-I" title="Did You Feel It? ma +ximum reported intensity (0 reports)">DYFI? - <strong class="roman">I +</strong></a></p><dl><dt>Time</dt><dd>2013-06-10 14:21:17 UTC</dd><dd +>2013-06-10 09:21:17 -05:00 at epicenter</dd><dt>Location</dt><dd>5.5 +45°S 81.572°W</dd><dt>Depth</dt><dd>47.92 km (29.78 mi)</dd>< +/dl> ]]> </summary> <georss:point>-5.5453 -81.5723</georss:point> <georss:elev>-47920</georss:elev> <category label="Age" term="Past Day"/> <category label="Magnitude" term="Magnitude 4"/> </entry>
and then my code:
thoughts?#!/usr/bin/perl -w use XML::FeedPP; use XML::LibXML; use XML::Rules; use LWP::Simple; use strict; use warnings; # get the Atom feed from the USGS my $source = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary +/4.5_day.atom'; my $feed = XML::FeedPP->new( $source ); # 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. my @rules = ( _default => sub {$_[0] => $_[1]->{_content}}, id => sub {$_[0] => $_[1]->{_content}}, author => undef, # ignorning the author because I already kno +w it's the USGS updated => sub {print "$_[1]->{updated}\n"; }, ); my $parser = XML::Rules->new(rules => \@rules); $parser->parse( $feed ); # 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); # my $id = $quake->get( $id ); # note that my attempt to get fiel +d data did not work. # my $update = $quake->updated(); # my $location = $quake->'georss:point'(); print "Magnitude ", $magnitude, " about ", $place, "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: One more parsing ATOM question
by frozenwithjoy (Priest) on Jun 11, 2013 at 02:30 UTC | |
by mcoblentz (Scribe) on Jun 11, 2013 at 11:23 UTC | |
|
Re: One more parsing ATOM question
by jakeease (Friar) on Jun 11, 2013 at 08:38 UTC | |
|
Re: One more parsing ATOM question
by Jenda (Abbot) on Jun 11, 2013 at 13:11 UTC | |
by mcoblentz (Scribe) on Jun 14, 2013 at 23:06 UTC | |
by Jenda (Abbot) on Jun 14, 2013 at 23:55 UTC |