Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Parsing NWS Public Alerts in CAP/ATOM

by johnfl68 (Scribe)
on Jul 22, 2012 at 23:05 UTC ( [id://983090]=perlquestion: print w/replies, xml ) Need Help??

johnfl68 has asked for the wisdom of the Perl Monks concerning the following question:

Has anyone had any luck finding a way to parse the NWS Public Alert feeds in CAP/ATOM?

It seems the NWS uses yet another malformed version of a standard for this, that doesn't seem to play well with parsing.

I have tried a few different parsers, but not finding anything that lets me pull the data out like like I have been able to with other XML files.

I just wondered if anyone else here has tried this, and found any approach that works.

Thanks!

John

Replies are listed 'Best First'.
Re: Parsing NWS Public Alerts in CAP/ATOM
by Anonymous Monk on Jul 22, 2012 at 23:58 UTC

    I have tried a few different parsers

    Such as?

    malformed version of a standard

    Malformed in what way? Which URL exactly?

    I just wondered if anyone else here has tried this, and found any approach that works.

    XML::Feed works for me

Re: Parsing NWS Public Alerts in CAP/ATOM
by johnfl68 (Scribe) on Jul 23, 2012 at 03:49 UTC

    Ok

    So here is what I have:

    #!/usr/bin/perl use strict; use warnings; use LWP::Simple; use XML::Simple; # use XML::LibXML; use POSIX qw(strftime); no warnings 'uninitialized'; use Data::Dumper; my $data_dir = "/weatherdata"; my $current_file = 'SDC095.xml'; my $current = XMLin($current_file); # These seem to work if there is no active alert # my $warntitle = $current->{'entry'}->{'title'}; # my $warnlink = $current->{'entry'}->{'id'}; # my $warntext = $current->{'entry'}->{'summary'}; # These work if I already know the full URL for the alert my $warntitle = $current->{'entry'}->{'http://alerts.weather.gov/cap/w +wacapget.php?x=SD124CB88C681C.SpecialWeatherStatement.124CB88C9184SD. +UNRSPSUNR.046ee1fda521a403079118865edf9ad4'}->{'title'}; my $warnlink = $current->{'entry'}->{'http://alerts.weather.gov/cap/ww +acapget.php?x=SD124CB88C681C.SpecialWeatherStatement.124CB88C9184SD.U +NRSPSUNR.046ee1fda521a403079118865edf9ad4'}->{'id'}; my $warntext = $current->{'entry'}->{'http://alerts.weather.gov/cap/ww +acapget.php?x=SD124CB88C681C.SpecialWeatherStatement.124CB88C9184SD.U +NRSPSUNR.046ee1fda521a403079118865edf9ad4'}->{'summary'}; print "$warntitle\n"; print "$warnlink\n"; print "$warntext\n"; # print output # print Dumper($current);

    And here is the XML file:

    https://dl.dropbox.com/u/2859483/SDC095.xml

    It wont let me post it as code in this message

    So, usually if there is no warnings in the area, I get the data ok, but in this case, instead of the second tier being 'title' 'id' and 'summary' those are being pushed to the third tier, and the second tier key is now 'http://alerts.weather.gov/cap/wwacapget.php?x=SD124CB88C681C.SpecialWeatherStatement.124CB88C9184SD.UNRSPSUNR.046ee1fda521a403079118865edf9ad4' and since I do not know that in advance, it's hard to get results that way.

    John

      Try stripping the problematic XML down to something that shows the issue, but is small enough to post. Also strip everything out of your sample code that's not related to the issue. Doing this has two benefits: the less work we have to do to help you the more likely you are to get help, and it is fairly likely that in the process of tidying up for posting that you will find the problem yourself.

      Note that XML::Simple is not really a general purpose XML parser and often comes unstuck for "interesting" XML. You may be better to use XML::Twig or one of the other "less simple" XML parsers.

      True laziness is hard work

      Well XML::Feed has way too many dependencies that are not on my server (shared server), so I didn't install because it needed too many other mods. XML::Twig installed fine, but wow, it does way too much (I'm sure that is great for more complex things), and I am having a hard time trying to figure out how to even parse a simpler current weather conditions xml file with out it coming up with errors like "Use of uninitialized value $temp_F in concatenation (.) or string at parsetest.pl" And it is getting really frustrating. I was have better luck with XML::Simple until I started trying to read the CAP Alerts. :(

      John

Re: Parsing NWS Public Alerts in CAP/ATOM
by Anonymous Monk on Jul 22, 2012 at 23:48 UTC
    Your description is rather vague. I suggest you link to the data and describe the specific errors or problems you encounter, and if possible, a simple working code example of what you have tried.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found