解析xml会导致html停止

时间:2015-01-15 02:45:25

标签: php html xml parsing

这是一个名为rockwell.php的html文档。循环时,如果$xmlData->checkerz[0]->attributes()->labro;,则PHP脚本有效,但$xmlData->checkerz[$x]->attributes()->labro;会阻止循环时加载其余的html。为什么这个循环解析会与html中的所有内容冲突?

<!DOCTYPE html>
<html lang="en">
...

    <?php

    header('Content-type: application/xml');

    $privatecode = 'thisz';

    $year = date("Y");
    $month = date("n");
    $day = date("d");

    $url =  'api.php?private='.$privatecode.'&day='.$day.'&month='.$month.'&year='.$year;
    $mis = file_get_contents($url);
    $xmlData = simplexml_load_string($mis);
    if (count($xmlData->checkerz) == 0) {
    }
    else {
    for ($x = 0; $x <= 5; $x++) {
    if ($x < count($xmlData->checkerz)) {  
    $timestampa = $xmlData->checkerz[$x]->attributes()->labro; //stops html here
    $timestampb = $xmlData->checkerz[$x]->attributes()->day;   //or here
    $timestampc = $xmlData->checkerz[$x]->attributes()->month; //or here
    $timestampd = $xmlData->checkerz[$x]->attributes()->year;  //or here
    if ($timestampc == '1') {
    $timestampe = 'January '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '2') {
    $timestampe = 'February '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '3') {
    $timestampe = 'March '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '4') {
    $timestampe = 'April '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '5') {
    $timestampe = 'May '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '6') {
    $timestampe = 'June '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '7') {
    $timestampe = 'July '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '8') {
    $timestampe = 'August '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '9') {
    $timestampe = 'September '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '10') {
    $timestampe = 'October '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '11') {
    $timestampe = 'November '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else if ($timestampc == '12') {
    $timestampe = 'December '.$timestampb.', '.$timestampa.'</br>';
    echo $timestampe;
    }
    else {
    }
    }
    }
    }
    ?>

...
</html>

2 个答案:

答案 0 :(得分:0)

因为这是PHP的工作方式,除非你做一些事情来分叉这个过程。您的脚本在单个执行线程中执行,该线程一步一步地执行您的代码。由于剩余的HTML在循环之后,因此在循环完成之前不会呈现。

答案 1 :(得分:0)

取而代之:

$timestampa = $xmlData->checkerz[$x]->attributes()->labro; //stops html here
    $timestampb = $xmlData->checkerz[$x]->attributes()->day;   //or here
    $timestampc = $xmlData->checkerz[$x]->attributes()->month; //or here
    $timestampd = $xmlData->checkerz[$x]->attributes()->year;

这解决了它:

if ($x == 0) {
$timestampa =  $xmlData->checkerz[0]->attributes()-> labro;
$timestampb =  $xmlData->checkerz[0]->attributes()->day;
$timestampc =  $xmlData->checkerz[0]->attributes()->month;
$timestampd =  $xmlData->checkerz[0]->attributes()->year;
}
else if ($x == 1) {
$timestampa =  $xmlData->checkerz[1]->attributes()-> labro;
$timestampb =  $xmlData->checkerz[1]->attributes()->day;
$timestampc =  $xmlData->checkerz[1]->attributes()->month;
$timestampd =  $xmlData->checkerz[1]->attributes()->year;
}
else if ($x == 2) {
$timestampa =  $xmlData->checkerz[2]->attributes()-> labro;
$timestampb =  $xmlData->checkerz[2]->attributes()->day;
$timestampc =  $xmlData->checkerz[2]->attributes()->month;
$timestampd =  $xmlData->checkerz[2]->attributes()->year;
}
else if ($x == 3) {
$timestampa =  $xmlData->checkerz[3]->attributes()-> labro;
$timestampb =  $xmlData->checkerz[3]->attributes()->day;
$timestampc =  $xmlData->checkerz[3]->attributes()->month;
$timestampd =  $xmlData->checkerz[3]->attributes()->year;
}
else if ($x == 4) {
$timestampa =  $xmlData->checkerz[4]->attributes()-> labro;
$timestampb =  $xmlData->checkerz[4]->attributes()->day;
$timestampc =  $xmlData->checkerz[4]->attributes()->month;
$timestampd =  $xmlData->checkerz[4]->attributes()->year;
}