phpsimplexmlxml-namespacescap

Parsing NWS XML Data - cap:geocode


I followed the info here:

Parse XML namespaces with php SimpleXML

And that works for everything except the information contained in the "cap:geocode" and "cap:parameter" entries.

$geocode = $entry->children('cap',true)->geocode;

returns an empty value.

Any ideas on how to get at the data inside of the cap:geocode and cap:parameter entries?

<cap:geocode>
    <valueName>FIPS6</valueName>
    <value>048017 048079</value>
    <valueName>UGC</valueName>
    <value>TXZ027 TXZ033</value>
</cap:geocode>

I need to read the ValueName/Value pairs.


Solution

  • I used this example here: https://github.com/tylerlane/php.news-leader.com/blob/master/weather/alerts.php

    And simplified it for my purposes to get this (echo.php just prints the data out):

    $dataFileName = "wx/CAP.xml";
    //load the feed
    $capXML = simplexml_load_file($dataFileName);
    //how many items
    $itemsTotal = count($capXML->entry);
    if(count($itemsTotal)):
    $capXML->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
    $result = $capXML->xpath("//prefix:entry");
    
    foreach($result as $capXML):
            $dc = $capXML->children('urn:oasis:names:tc:emergency:cap:1.1');
            $event = $dc->event;
            $effective = $dc->effective;
            $expires = $dc->expires;
            $status = $dc->status;
            $msgType = $dc->msgType;
            $category = $dc->category;
            $urgency = $dc->urgency;
            $severity = $dc->severity;
            $certainty = $dc->certainty;
            $areadesc = $dc->areaDesc;
            $geopolygon = $dc->polygon;
            //get the children of the geocode element
            $geocodechildren = $dc->geocode->children();
            //only interested in FIPS6 for now
            //no guarantee that FIPS6 will be the first child so we have to deal with that
            if($geocodechildren->valueName == "FIPS6"){
                //isolate all the FIPS codes
                $fips = explode( " ", $geocodechildren->value );
    
            } else {
                //hide everything else so we don't fail
                $fips = Array();
            }
            //get the VTEC
            $parameter_children = $dc->parameter->children();
            if($parameter_children->valueName == "VTEC"){
                //isolate all VTEC codes
                $vtec = explode( ".", $parameter_children->value );
    
            } else {
                //hide anything else that may show up
                $vtec = Array();
            }
    
            include('echo.php');
            print_r($fips);
            echo "<br/>";
            print_r($vtec);
            echo "<hr/>";
    endforeach;
    endif;