1

所以这是 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]但没有结果。

有什么想法可以检索和打印该国家/地区吗?谢谢!

4

1 回答 1

1

包含 PHP 变量名中不允许的字符的 XML 元素名必须用大括号括起来或放在变量的前面,例如$recordingList = 'recording-list'; $xml->$recordingList;. 此外,数组是从零开始的:

$xml->{'recording-list'}->recording[0]->{'release-list'}->release[0]->country;

http://codepad.org/Yhz3V3wE

于 2012-04-23T22:13:30.860 回答