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.

1
  • can you post a sample of the XML document? Commented May 17, 2011 at 7:14

1 Answer 1

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
    );
}
Sign up to request clarification or add additional context in comments.

8 Comments

I just tried to run this and its not printing the <value> element. Its outputting ID: alerts.weather.gov/cap/… VTEC: ID: alerts.weather.gov/cap/… .....
@Zachary it does what you asked for. See demo link.
@Gordon... it does not return the $entry->children('cap', true)->parameter->children()->value
@Zachary It returns all /feed/entry/id and /feed/entry/cap:parameter/value
@Gordon... my appologies... I pasted it wrong. Thanks for the help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.