0

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.

Community
  • 1
  • 1
dnavarrojr
  • 19
  • 5
  • You have missed to show the XML. Only using a namespace prefix on some elements children *must not mean you're using it on the right `$entry`*. Most likely you've just made a little mistake, that's all. – hakre Apr 27 '14 at 09:09
  • @hakre I added the XML from the linked article. – dnavarrojr Apr 29 '14 at 06:43
  • Is that the whole XML or is it an excerpt? If an excerpt, please add (at least exemplary/shortened) the XML represented by `$entry` including the parent element of it. – hakre Apr 29 '14 at 10:24
  • The entire XML is on the original article I linked to at the top of my question. I cut and pasted that excerpt from that article. http://stackoverflow.com/questions/16412047/parse-xml-namespaces-with-php-simplexml – dnavarrojr Apr 29 '14 at 23:43

2 Answers2

1

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;
jyllstuart
  • 83
  • 9
0

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

The key point in your case is, that the XML provided in the other question is invalid.

You would have noticed that if you had followed a good practice in PHP development: Enable reporting of errors, warning and notices to the highest level, display those as well as log those to file. Then track those warnings.

In your case you should have seen some message like:

Warning: simplexml...: namespace error : Namespace prefix cap on event is not defined in /path/to/script.php on line 42

This is a notice that the cap XML namespace prefix is undefined. That means that SimpleXML will drop it. Those elements are then put into the default namespace of the document so that you can access them directly.

So first of all make yourself comfortable with setting your php.ini file on your development system for development error reporting so that you'll be noticed about unexpected input values. One stop for that is the following question:

Next to that you need to decide why the input is wrong and how you'd like to deal with errors. Should it fail (the design of XML suggest to go with the fail route which is also considered a design issue for XML) or do you want to "repair" the XML or do you want to work with the invalid XML. That decision is up to. SimpleXML does work as announced, it's just in your case you got the error unnoticed and you're not doing any error handling so far.

The same problem with similar XML has been asked/answered about previously:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • So, is this something that I need to report to the National Weather Service? That they are generating invalid XML? Clearly they believe it's valid, or they wouldn't generate it that way. – dnavarrojr May 06 '14 at 22:41
  • Bla bla bla. Clearly. Hmm. Clearly clearly. Bla bla bla. – hakre May 06 '14 at 23:01