1

我在尝试遍历 XML 文档时遇到了一些麻烦。XML 如下所示:

<data>
<weather>
    <hourly>
        <time>0</time>
        <tempC>17</tempC>
        <tempF>62</tempF>
        <windspeedMiles>24</windspeedMiles>
        <windspeedKmph>39</windspeedKmph>
    </hourly>
    <hourly>
        <time>3</time>
        <tempC>16</tempC>
        <tempF>60</tempF>
        <windspeedMiles>22</windspeedMiles>
        <windspeedKmph>35</windspeedKmph>
    </hourly>
</weather>
<weather>
    <hourly>
        <time>0</time>
        <tempC>17</tempC>
        <tempF>62</tempF>
        <windspeedMiles>24</windspeedMiles>
        <windspeedKmph>39</windspeedKmph>
    </hourly>
    <hourly>
        <time>3</time>
        <tempC>16</tempC>
        <tempF>60</tempF>
        <windspeedMiles>22</windspeedMiles>
        <windspeedKmph>35</windspeedKmph>
    </hourly>
</weather>
</data>

我的代码(如下)在遍历所有“天气”节点时,它只挑选出第一个“每小时”子节点并完全跳过第二个。有人能像我一样诚实地帮助我吗?我对循环修复它知之甚少,它让我发疯!Grr。

这是我的 PHP 代码,它从在线加载 XML 文档,然后将 XML 结果格式化为 div 标记,显然循环遍历 XML,但正如我所说,仅循环遍历每个“天气”节点的第一个“每小时”节点。

<?php
// load SimpleXML
$data = new SimpleXMLElement('myOnlineXMLdocument.xml', null, true);
echo <<<EOF
<div class="observationRow">
    <div class="observationTitleSmall"><br>Time</div>
    <div class="observationTitleSmall"><br>Temp C</div>
    <div class="observationTitleSmall"><br>Temp F</div>
    <div class="observationTitleSmall"><br>Wind Speed MPH</div>
    <div class="observationTitleSmall"><br>Wind Speed KMPH</div>
</div>
EOF;
foreach($data as $weather) // loop through our hours
{
echo <<<EOF
    <div>
        <div class="observationCellSmall"><br>{$weather->time}</div>
        <div class="observationCellSmall"><br>{$weather->tempC}</div>
        <div class="observationCellSmall"><br>{$weather->tempF}</div>
        <div class="observationCellSmall"><br>{$weather->hourly->windspeedMiles}</div>
        <div class="observationCellSmall"><br>{$weather->hourly->windspeedKmph}</div>
EOF;
}
echo    '</div>';
?>
4

2 回答 2

1

编辑代码:

$str = "";
foreach($data->weather as $weather)
{
    foreach ($weather->hourly as $hour)
    {
        $str .= "
    <div>";

        if ($hour->time == "0") {
            $str .= "
        <div class='observationCellSmall'><br>$weather->date</div>
        <div class='observationCellSmall'><br>$weather->maxtempC</div>
        <div class='observationCellSmall'><br>$weather->mintempC</div>";
        }

        $str .= "
        <div class='observationCellSmall'><br>$hour->time</div>
        <div class='observationCellSmall'><br>$hour->tempC</div>
        <div class='observationCellSmall'><br>$hour->tempF</div>
        <div class='observationCellSmall'><br>$hour->windspeedMiles</div>
        <div class='observationCellSmall'><br>$hour->windspeedKmph</div>
    </div>
    ";
    }
}
echo $str;

使用 XML 提要的细化版本,生成以下内容:

   <div>
        <div class='observationCellSmall'><br>2013-08-19</div>
        <div class='observationCellSmall'><br>17</div>
        <div class='observationCellSmall'><br>15</div>
        <div class='observationCellSmall'><br>0</div>
        <div class='observationCellSmall'><br>15</div>
        <div class='observationCellSmall'><br>59</div>
        <div class='observationCellSmall'><br>11</div>
        <div class='observationCellSmall'><br>18</div>
    </div>

    <div>
        <div class='observationCellSmall'><br>300</div>
        <div class='observationCellSmall'><br>15</div>
        <div class='observationCellSmall'><br>59</div>
        <div class='observationCellSmall'><br>13</div>
        <div class='observationCellSmall'><br>21</div>
    </div>

    <div>
        <div class='observationCellSmall'><br>2013-08-20</div>
        <div class='observationCellSmall'><br>21</div>
        <div class='observationCellSmall'><br>16</div>
        <div class='observationCellSmall'><br>0</div>
        <div class='observationCellSmall'><br>17</div>
        <div class='observationCellSmall'><br>62</div>
        <div class='observationCellSmall'><br>11</div>
        <div class='observationCellSmall'><br>18</div>
    </div>

    <div>
        <div class='observationCellSmall'><br>300</div>
        <div class='observationCellSmall'><br>16</div>
        <div class='observationCellSmall'><br>61</div>
        <div class='observationCellSmall'><br>10</div>
        <div class='observationCellSmall'><br>17</div>
    </div>
于 2013-08-18T23:38:05.183 回答
1

你需要一个嵌套循环。一个循环天气,另一个循环每小时。

foreach($data->weather as $weather) {
   foreach($weather->hourly as $hourly) {
     // code here
   }
}

我不记得 100% 的 simplexml API,如果这不起作用,您可能需要使用->getChildren()或其他东西使其可迭代。

要么这样,要么直接使用 xpath 和 nab 小时表:/data/weather/hourly.

于 2013-08-18T23:41:31.550 回答