的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 –