所以这是musicbrainz返回的一个例子xml(艺术家“Absynthe Minded”和歌曲“Moodswing Baby”):
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<recording-list offset="0" count="3">
<recording ext:score="100" id="580d3e6f-6b2a-4f2c-bb06-a73486de50b8">
<title>Moodswing Baby</title>
<length>230706</length>
<artist-credit>...</artist-credit>
<release-list>
<release id="f183e8a3-5326-44c9-8ab3-00a2dbb74caf">
<title>Absynthe Minded</title>
<status>Official</status>
<release-group type="Album"/>
<date>2009</date>
<country>BE</country>
<medium-list>...</medium-list>
</release>
<release id="fccd0f03-30e5-44ac-8825-a61ad8b25442">...</release>
<release id="b3873ea0-0ed3-48f1-9703-4f5302460d0e">...</release>
<release id="b68fb38c-5a5a-4d24-bd41-2c50945cef39">...</release>
</release-list>
<puid-list>...</puid-list>
<isrc-list>...</isrc-list>
</recording>
<recording ext:score="100" id="359c27e3-5153-445e-aa66-71d8152d19e4">...</recording>
<recording ext:score="31" id="e9e4628a-61ff-4dd7-8fbb-a8d655d29a5a">...</recording>
</recording-list>
</metadata>
我正试图在我的网站上打印这个国家“BE”旁边的艺术家名字:
<?php
$xml = simplexml_load_file("http://www.musicbrainz.org/ws/2/recording?query=moodswing%20baby%20AND%20artist:absynthe%20minded");
$info = $xml->recording-list->recording[1]->release-list->release[1]->country;
echo $info;
?>
我使用相同的方法从last.fm的数据库(也是xml)中检索歌曲和艺术家信息但它似乎不适用于此(T_OBJECT_OPERATOR,期待'('错误)。我试图删除[1]但没有结果。
我是如何检索和打印该国家的?谢谢!
答案 0 :(得分:1)
包含PHP变量名称中不允许的字符的XML元素名称必须首先用大括号括起来或放在变量中,例如$recordingList = 'recording-list'; $xml->$recordingList;
。此外,数组是从零开始的:
$xml->{'recording-list'}->recording[0]->{'release-list'}->release[0]->country;