如何遍历XML的多个子节点?

时间:2013-08-18 23:14:10

标签: php xml loops

我在尝试遍历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,但正如我所说,只循环遍历每个'weather'节点的第一个'每小时'节点。

<?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>';
?>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

您需要一个嵌套循环。一个环绕天气,另一个环绕小时。

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

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

或者直接使用xpath和nab the hourlies:/data/weather/hourly