質問

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.

役に立ちましたか?

解決

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;

他のヒント

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:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top