0

我已经像这样从 XML 文件中检索数据

$response = simplexml_load_file($url);

print_r 显示以下内容

SimpleXMLElement Object
(
[artist] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [type] => Group
                [id] => b9fb5447-7f95-4a6a-a157-afed2d7b9f4c
            )

        [name] => He Is Legend
        [sort-name] => He Is Legend
        [country] => US
        [area] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 489ce91b-6658-3307-9877-795b68554c98
                    )

                [name] => United States
                [sort-name] => United States
                [iso-3166-1-code-list] => SimpleXMLElement Object
                    (
                        [iso-3166-1-code] => US
                    )

            )

    )

)

所以我可以输出名称和国家等字段

echo $response->artist->name;
echo $response->artist->country;

但是,当谈到能够访问属性数组中的数据时,我被困住了。例如,如何从第一个属性数组中获取组的类型?

编辑

我也试图从这样的函数中返回详细信息

func getDetails($id) {

$response = simplexml_load_file('http://musicbrainz.org/ws/2/artist/'.$id);
$data = array();
$data['type'] = $response->artist->attributes()->type;
$data['country'] = $response->artist->country;

return $data;
}

print_r(getDetails());

给我

MusicBrainz Object
(
)
4

1 回答 1

1

您可以使用以下attributes()方法访问它们:

echo $response->artist->attributes()->type;

SimpleXML 文档中的示例 #5显示了另一个示例。

于 2013-11-09T18:57:52.777 回答