2012-11-20 31 views
2

我有xml。 這是電影放映時間,我嘗試解析。

<shows> 
    <show id="160575" film_id="7043" cinema_id="89" hall_id="241"> 
     <begin>2012-11-15</begin> 
     <end>2012-11-18</end> 
     <times> 
      <time time="10:30:00"> 
       <prices>20, 30</prices> 
       <note><![CDATA[]]></note> 
      </time> 
     </times> 
    </show> 
</shows> 

我分析我的XML和回聲內容: 字符串 「film_id_m」 必須是7043從標籤。

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

///////////string/////////////////// 
$begin_m = $cinema[0]->begin; 
$end_m = $cinema[0]->end; 
$film_id_m = ????????; 



/////////echo////////////////////// 
echo "<b>BEGIN: </b>".$begin_m."<br>"; 
echo "<b>END: </b>".$end_m."<br>"; 
echo "<b>FILM ID: </b>".$film_id_m."<br>"; 

對不起,我的英語不好。

+0

'$影院[0] - > attributes(「film_id」)' – didierc

+0

[SimpleXmlElement attributes](http://php.net/manual/en/simplexmlelement.attributes.php):) – didierc

回答

2

由於film_id爲當前處理元素的屬性,您可以檢索其值與任...

$film_id_m = $cinema[0]->attributes()->film_id; 

...或只是...

$film_id_m = $cinema[0]['film_id'];