【问题标题】:Parsing xml causes html to stop解析 xml 导致 html 停止
【发布时间】:2015-01-15 02:45:25
【问题描述】:

这是在一个名为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>

【问题讨论】:

  • 那么它在哪个迭代上失败了?也许您在 $x 中存在某种差异,或者可能缺少某个属性。你能用 XML 更新你的问题吗?同时打开dispaly_errors 或检查您的日志,让我们知道错误消息是什么。

标签: php html xml parsing


【解决方案1】:

因为这就是 PHP 的工作方式,除非您对进程进行分叉。您的脚本在单个执行线程中执行,该线程一次执行您的代码。由于剩余的 HTML 在循环之后,因此在循环完成之前不会呈现。

【讨论】:

    【解决方案2】:

    替换这个:

    $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;
    }
    

    【讨论】:

    • 那是一种可怕的处理方式......你应该修复你的循环。例如,jsut 使用foreach 并维护自己的计数器,因为我很确定这只是循环索引的问题。
    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    相关资源
    最近更新 更多