Dear monks,

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&deg;S 81.572&deg;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:

#!/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"; }

thoughts?

In reply to One more parsing ATOM question by mcoblentz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.