我正在阅读的 XML 如下所示:
<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;
这很好用。但是我该怎么做才能获得ID<show id="8511">
?
我尝试过类似的事情:
$id = $xml->show;
$id = $xml->show[0];
但没有一个奏效。
Update
我的代码片段:
$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
这应该有效。
$id = $xml["id"];
您的 XML 根成为 SimpleXML 对象的根;您的代码正在通过名称“show”调用子根,该根不存在。
您还可以使用此链接获取一些教程:http://php.net/manual/en/simplexml.examples-basic.php http://php.net/manual/en/simplexml.examples-basic.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)