的XML我正在讀看起來像這樣:PHP的SimpleXML +獲取屬性
<show id="8511">
<name>The Big Bang Theory</name>
<link>http://www.tvrage.com/The_Big_Bang_Theory</link>
<started>2007-09-24</started>
<country>USA</country>
<latestepisode>
<number>05x23</number>
<title>The Launch Acceleration</title>
</latestepisode>
</show>
要獲得(例如)在最新一集的數量,我會做:
$ep = $xml->latestepisode[0]->number;
該作品正好。但是,如何從<show id="8511">
獲得ID?
我已經試過類似:
$id = $xml->show;
$id = $xml->show[0];
但沒有奏效。
更新
我的代碼片段:
$url = "http://services.tvrage.com/feeds/episodeinfo.php?show=".$showName;
$result = file_get_contents($url);
$xml = new SimpleXMLElement($result);
//still doesnt work
$id = $xml->show->attributes()->id;
$ep = $xml->latestepisode[0]->number;
echo ($id);
大利。 XML:
http://services.tvrage.com/feeds/episodeinfo.php?show=The.Big.Bang.Theory
http://php.net/ simplexml.examples-basic – hakre
可能的重複[從SimpleXML訪問@attribute](http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) - 供參考。 – hakre
請參閱: http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute/19289857#19289857 –