Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: One more parsing ATOM question

by jakeease (Friar)
on Jun 11, 2013 at 08:38 UTC ( [id://1038222]=note: print w/replies, xml ) Need Help??


in reply to One more parsing ATOM question

Inspired by frozenwithjoy, I played with it a little, arriving at

#!/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, # ignoring the author because I already know + it's the USGS author => sub {$_[0] => $_[1]->{_content}}, updated => sub {print "$_[1]->{updated}\n"; }, ); my $parser = XML::Rules->new(rules => \@rules); 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); my $id = $quake->get( "id"); my $update = $quake->get( "updated" ); my $location = $quake->get( "georss:point"); print "Magnitude ", $magnitude, " about ", $place, "\n"; print "id is $id, updated at $update, georss:point is $location \n +"; }

Running it,

C:\Users\jkeys>perl c:\myperl\usgsfeed_norules.pl Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Use of uninitialized value in concatenation (.) or string at c:\myperl +\usgsfeed_norules.pl line 25. Magnitude 4.9 about 67km SW of Painan, Indonesia id is urn:earthquake-usgs-gov:us:c000hl79, updated at 2013-06-11T03:00 +:47.746Z, georss:point is -1.8063 100.1636 Magnitude 4.7 about 131km NNE of Calama, Chile id is urn:earthquake-usgs-gov:us:c000hl5v, updated at 2013-06-11T03:13 +:17.731Z, georss:point is -21.3693 -68.4452 Magnitude 4.9 about Galapagos Triple Junction region id is urn:earthquake-usgs-gov:us:c000hl36, updated at 2013-06-11T06:39 +:02.261Z, georss:point is 1.3674 -101.2936 Magnitude 5.0 about 75km ESE of Pangai, Tonga id is urn:earthquake-usgs-gov:us:c000hl2w, updated at 2013-06-11T06:31 +:30.385Z, georss:point is -20.0901 -173.6971 Magnitude 4.9 about 71km SW of Paita, Peru id is urn:earthquake-usgs-gov:us:c000hkbe, updated at 2013-06-10T22:23 +:52.275Z, georss:point is -5.5453 -81.5723 Magnitude 4.7 about 68km E of Sarangani, Philippines id is urn:earthquake-usgs-gov:us:c000hk85, updated at 2013-06-10T19:54 +:48.457Z, georss:point is 5.2973 126.0703 Magnitude 4.6 about 60km WNW of Lata, Solomon Islands id is urn:earthquake-usgs-gov:us:c000hk60, updated at 2013-06-10T16:28 +:39.451Z, georss:point is -10.4972 165.3268

This provides a few examples of accessors. I didn't attempt to dive into the rules, other than to comment out the $author => undef which quieted some of the uninitialized value warnings.

Hope this helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1038222]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-26 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found