dpsx99068 2013-11-09 18:49
浏览 29

如何从Simple XML Object获取某些详细信息?

I've retrieved data from an XML file like so

$response = simplexml_load_file($url);

print_r displays the following

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
                    )

            )

    )

)

So I can output fields like name and country by

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

However I'm stuck when it comes to being able to access data in the attribute arrays. How can I get the type of group from the first attributes array for example?

Edit

I'm also trying to return the details from a function like so

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());

Gives me

MusicBrainz Object
(
)
  • 写回答

1条回答 默认 最新

  • dru5089 2013-11-09 18:57
    关注

    You can access them using the attributes() method:

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

    The example #5 in the SimpleXML documentation shows another example.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)