PHP SimpleXML命名空间问题

Zac*_*ter 4 php xml simplexml

我正在尝试获取RSS提要中每个条目的entry-> id和entry-> cap:parameter->值....下面是我正在使用的代码.它正确显示id,但它没有显示值字段....请帮忙.

$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>";
}
Run Code Online (Sandbox Code Playgroud)

我在这里先向您的帮助表示感谢.

Gor*_*don 7

<value>元素是不是在同一个命名空间<cap:parameter>:

<cap:parameter>
    <valueName>VTEC</valueName>
    <value>/O.CON.KMPX.FL.W.0012.000000T0000Z-110517T1800Z/</value>
</cap:parameter>
Run Code Online (Sandbox Code Playgroud)

所以你必须children()再次打电话.

代码(演示)

$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
    );
}
Run Code Online (Sandbox Code Playgroud)