2013-08-18 50 views
0

我目前遇到问题,我的laravel站点正在返回错误。我使用下面的代码:Laravel 4/SimpleXML/PHP节点不再存在

@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,我wan't使用$list->world->children()但它不为我工作。当我尝试运行我的网页,我得到以下错误:

main(): Node no longer exists 

而且,这里是我的XML文件:http://paste.laravel.com/Koh

我可以使用它,而不->world但后来,因为你在我的XML文件中看到,它就像我的表格中的<td>一样。

+0

有人吗?请? :P –

+0

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

回答

1

啊,我看到你的问题;你试图从第一个子元素(即<config>)获取属性(maxlevel等),并且这些元素只有一个'enabled'属性。 尝试一下;

<?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; 
    } 
} 
+0

谢谢,但它需要从文件中加载它。你能从我的剧本中完成吗?我现在很困惑,我可以在我的剧本中做到吗? –

+0

您仍然可以使用'$ list = simplexml_load_file($ stagesdir。'\ stages.xml');'。我只使用了'simplexml_load_string'作为示例,向您展示了如何在foreach循环中使用$ child(或$ stage)变量。 –

+0

是的,但它现在显示配置启用,如果它是0或1.我可以禁用它,什么都不显示? :p –