【问题标题】:Laravel 4/SimpleXML/PHP node no longer existsLaravel 4/SimpleXML/PHP 节点不再存在
【发布时间】:2013-08-18 14:29:13
【问题描述】:

我目前有一个问题,我的 laravel 网站返回错误。我正在使用以下代码:

    @extends('base')
@section('title', 'Experience stages')
@stop
@section('main')

<div class="container">
  <div class="doc-content-box">

    <legend>Experience stages</legend>


    <?php   

    $stagesdir = 'C:\Users\E1\Desktop\theforgottenserver-v0.2.15-win64console\Mystic Spirit\data\XML';   

    if(is_dir($stagesdir)) {   
      $list = simplexml_load_file($stagesdir.'\stages.xml'); 

      ?> 
      <table class="table table-striped">

        <thead>
            <tr>
                <th>From Level</th>
                <th>To Level</th>
                <th>Experience multiplier</th>
            </tr>
        </thead>

      <tbody>
        <?php  
        foreach($list->children() as $stage) {  
            if ($stage['maxlevel'] == '') { 
        $stage['maxlevel'] = '*';     
        } 
            echo '  
            <tr>  
            <td>'.$stage['minlevel'].'</td>
            <td>'.$stage['maxlevel'].'</td>  
            <td style="width: 30%">'.$stage['multiplier'].'x</td>    
            </tr>';
        }  
      }
      else{
        echo '<div class="alert alert-danger"><span class="label label-important">Error parsing your Monsters XML file</span><br /><br /> Invalid path!</div> ';
      }  
      ?>  
    </tbody>

  </table> 

</div>
</div>

@stop

在那个 foreach 中,我不想使用 $list-&gt;world-&gt;children(),但它对我不起作用。当我尝试运行我的页面时,我收到以下错误:

main(): Node no longer exists

另外,这是我的 XML 文件:http://paste.laravel.com/Koh

我可以在没有 -&gt;world 的情况下使用它,但是正如您在我的 XML 文件中看到的那样,它就像我的表中的 &lt;td&gt;

【问题讨论】:

  • 错误发生在哪一行(数字或代码)?

标签: php xml laravel simplexml laravel-4


【解决方案1】:

啊,我明白你的问题了;您试图从第一个子元素(&lt;config&gt;)获取属性(maxlevel 等),并且该元素只有一个“启用”属性。 试试这个;

<?php
$xml = '<stages>
<config enabled="0"/>
<stage minlevel="1" maxlevel="8" multiplier="7"/>
<stage minlevel="9" maxlevel="20" multiplier="6"/>
<stage minlevel="21" maxlevel="50" multiplier="5"/>
<stage minlevel="51" maxlevel="100" multiplier="4"/>
<stage minlevel="101" multiplier="5"/>
</stages>';
$list = simplexml_load_string($xml);

echo "<pre>";
foreach($list->children() as $child)
{
    switch($child->getName())
    {
        case 'config':
            echo 'Config->enabled: ';
            echo (string)$child['enabled'];
            echo "\n";
            break;
        case 'stage':
            echo 'Stage->minlevel: ';
            echo (string)$child['minlevel'];
            echo "\n";
            break;
    }
}

【讨论】:

  • 谢谢,但需要从文件中加载它。你能用我的脚本来做吗?我现在很困惑,我可以在我的脚本中这样做吗?
  • 您仍然可以使用$list = simplexml_load_file($stagesdir.'\stages.xml');。我仅在示例中使用了simplexml_load_string,该示例向您展示了如何在 foreach 循环中使用 $child(或 $stage)变量。
  • 是的,但是如果它是 0 或 1,它现在会显示启用的配置。我可以禁用它,什么都不显示吗? :p
  • 1) 您可以将 config 的 case 留空,因此它不会显示任何内容,或者 2) 在您原来的 foreach 循环中,在打开循环后添加 if($stage-&gt;getName() != 'stage') { continue; } - 这将跳过配置节点....
猜你喜欢
  • 2011-01-30
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
相关资源
最近更新 更多