#!/usr/bin/perl use strict; use LWP::Simple; use XML::LibXML; #use XML::LibXML::XPathContext; use CGI ":cgi"; print '' . "\n"; # should be wqx_programs? print "\n"; my %state_codes = ( '23' => 'ME', '33' => 'NH', '25' => 'MA', '90' => 'NB', '44' => 'RI', '09' => 'CT', ); my $parser = XML::LibXML->new(); my $base_xml_url = 'http://qwwebservices.usgs.gov/Station/search?mimeType=xml&siteType=Estuary&statecode='; while ( my ($st_code,$state) = each %state_codes) { my $xml_url = $base_xml_url . $st_code; my $the_xml = LWP::Simple::get($xml_url); if( not $the_xml){ warn "No response $st_code\n"; next; } if($the_xml =~ /ExceptionReport/i ) { warn "Exception: " . $the_xml; next; } my $pxml = $parser->parse_string($the_xml); if(not $pxml) { warn "Could not parse $xml_url\n"; } my $xc = XML::LibXML::XPathContext->new($pxml); # get namespaces from the root element and register them my $root = $pxml->documentElement(); my @attributes = $root->attributes(); foreach my $a (@attributes) { # we only care about the xmlns next unless $a->getName =~ /xmlns/; ; # xmlns:sos e.g. my @tmp = split( ":", $a->getName); my $url = $a->getData; # this maps the xmlns prefix to the value which should be a URL if( not $tmp[1]){ $xc->registerNs( 'my' => $url); }else{ $xc->registerNs( $tmp[1] => $url); } } # end foreach root attributes # get all MonitoringLocations my $org_cnt = 1; foreach my $node ( $xc->findnodes('//my:WQX/my:Organization') ) { my $ml_cnt = 1; my $org_id = $xc->find('my:OrganizationDescription/my:OrganizationIdentifier', $node)->string_value; my $org_name = $xc->find('my:OrganizationDescription/my:OrganizationFormalName', $node)->string_value; foreach my $node2 ( $xc->findnodes('my:MonitoringLocation', $node) ) { my $xml = ''; my $ml_name = $xc->find('my:MonitoringLocationIdentity/my:MonitoringLocationName', $node2)->string_value; my $ml_id = $xc->find('my:MonitoringLocationIdentity/my:MonitoringLocationIdentifier', $node2)->string_value; my $ml_type = $xc->find('my:MonitoringLocationIdentity/my:MonitoringLocationTypeName', $node2)->string_value; my $ml_lat = $xc->find('my:MonitoringLocationGeospatial/my:LatitudeMeasure', $node2)->string_value; my $ml_lon = $xc->find('my:MonitoringLocationGeospatial/my:LongitudeMeasure', $node2)->string_value; my $ml_st_code = $xc->find('my:MonitoringLocationGeospatial/my:StateCode', $node2)->string_value; my $st = $state_codes{$ml_st_code}; my $ml_cty_code = $xc->find('my:MonitoringLocationGeospatial/my:CountyCode', $node2)->string_value; my $title = "$ml_name $st"; $xml .= '\n"; exit;