2012-11-21 92 views
1

我有XML文件。這是電影放映時間,我嘗試解析。XML解析器PHP.Syntax錯誤?

<?xml version="1.0" encoding="UTF-8"?> <billboard> 
    <genres> 
<shows> 
<show id="160576" film_id="5710" cinema_id="89" hall_id="241"> 
       <begin>2012-11-15</begin> 
       <end>2012-11-18</end> 
       <times> 
              <time time="12:30:00"> 
          <prices>30, 55</prices> 
          <note><![CDATA[]]></note> 
         </time> 
              <time time="14:45:00"> 
          <prices>30, 55</prices> 
          <note><![CDATA[]]></note> 
         </time> 
              <time time="17:00:00"> 
          <prices>30, 55</prices> 
          <note><![CDATA[]]></note> 
         </time> 
              <time time="23:45:00"> 
          <prices>30, 55</prices> 
          <note><![CDATA[]]></note> 
         </time> 
            </times> 
      </show> 

我分析我的XML和回聲content.But我不能回聲「時間」和「價格」。之後插入代碼「//回波時間和價格//」腳本不起作用(白屏) 。請幫忙。 P.S.對不起我的英語不好。

$xmlstr = file_get_contents('test.xml'); 
$x = new SimpleXMLElement($xmlstr); 
$id_f = 89; 
$cinema_id = "//show[@cinema_id=".$id_f."]"; 

$cinema=$x->xpath($cinema_id); 
///////////////////////start///////////////////////////////////////////// 
foreach($cinema as $cinema) 
     { 
///////////string/////////////////// 
$begin_m = $cinema[0]->begin; 
$end_m = $cinema[0]->end; 
$film_id_m = $cinema[0]['film_id']; 


/////////echo////////////////////// 
echo "<b>Begin: </b>".$begin_m."<br>"; 
echo "<b>End: </b>".$end_m."<br>"; 
echo "<b>ID film: </b>".$film_id_m."<br>"; 
/////////echo time and price/// 

    foreach($cinema[0]->times->time as $k){ 
    $obj=current($k); 
    $time_b = $obj['time']."\n"; 

    echo "Time: "$time_b."<br>"; 
    foreach($cinema[0]->times as $price){ 

    echo "Price: "$price[0]->pices."<br>"; 



} 
///////////// 

    echo "<hr>"; 
} 

} 

回答

1

有在你的代碼的語法錯誤,導致500內部服務器錯誤(顯示了白屏)。

語法錯誤1:

echo "Time: ".$time_b."<br>"; 
      //^you were missing this 

語法錯誤2:

echo "Price: ".$price[0]->pices."<br>"; 
      //^you were missing this 

而且,在你的foreach循環,您指定的元素相同的變量名,你的循環數組。這不會導致錯誤,但這意味着你的$cinema數組在循環之後死了,它將被設置爲最後一個元素的值。給它一個不同的名稱:

foreach($cinema as $cinemaItem) 

修正了這些問題,您的代碼將按照您的預期輸出。在未來,你應該檢查你的錯誤日誌或打開,因爲他們會告訴你在句法錯誤錯誤:

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
+0

謝謝你,我真的很感激 – parks

+0

@parks,沒問題:) – MrCode