4

I'm trying to get the entry->id and entry->cap:parameter->value for every entry in the RSS feed.... below is the code I'm using. It is displaying the id correctly however it is not displaying the value field.... please help.

$url = 'http://alerts.weather.gov/cap/us.php?x=1';
$cap = simplexml_load_file($url);
foreach($cap->entry as $entry){
    echo 'ID: ', $entry->id, "\n";
    echo 'VTEC: ', $entry->children('cap', true)->parameter->value, "\n"; 
echo "<hr>";
}

Thanks for the help in advance.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Zachary Lassiter
  • 181
  • 1
  • 2
  • 13

1 Answers1

7

The <value> element is not in the same namespace as <cap:parameter>:

<cap:parameter>
    <valueName>VTEC</valueName>
    <value>/O.CON.KMPX.FL.W.0012.000000T0000Z-110517T1800Z/</value>
</cap:parameter>

So you have to call children() again.

Code (demo)

$feed = simplexml_load_file('http://alerts.weather.gov/cap/us.php?x=1');
foreach ($feed->entry as $entry){
    printf(
        "ID: %s\nVTEC: %s\n<hr>",
        $entry->id,
        $entry->children('cap', true)->parameter->children()->value
    );
}
Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • I just tried to run this and its not printing the element. Its outputting ID: http://alerts.weather.gov/cap/wwacapget.php?x=AK20110517092700FireWeatherWatch20110519060000AK.AFGRFWAFG.e64a0affe933deb186dab792161df75f VTEC: ID: http://alerts.weather.gov/cap/wwacapget.php?x=AK20110517092700FireWeatherWatch20110519060000AK.AFGRFWAFG.27edc87d0c20aa820c20e6e07d509b56 ..... – Zachary Lassiter May 17 '11 at 14:26
  • @Zachary it does what you asked for. See demo link. – Gordon May 17 '11 at 14:31
  • @Gordon... it does not return the $entry->children('cap', true)->parameter->children()->value – Zachary Lassiter May 17 '11 at 14:56
  • @Zachary It returns all `/feed/entry/id` and `/feed/entry/cap:parameter/value` – Gordon May 17 '11 at 15:06
  • @Gordon.... it does not return the /feed/entry/cap:parameter/value.... try running it! – Zachary Lassiter May 17 '11 at 15:31
  • @Zachary I **did** run it. On my machine. On the Viper codepad and I asked someone else to run it. It does return what you are asking for: http://pastebin.com/vqCC3NBR – Gordon May 17 '11 at 15:41
  • 1
    @Gordon... my appologies... I pasted it wrong. Thanks for the help! – Zachary Lassiter May 17 '11 at 15:47
  • This is a hard answer to find among all the other simpleXML/namespace questions. But it was the correct one in my case. Thanks a lot. – Ryan Nov 02 '12 at 20:04