tags:

views:

25

answers:

1

Hi all.

I'm trying to parse this feed: http://musicbrainz.org/ws/1/artist/c0b2500e-0cef-4130-869d-732b23ed9df5?type=xml&inc=url-rels

I want to grab the URLs inside the 'relation-list' tag.

I've tried fetching the URL with PHP using simplexml_load_file(), but I can't access it using $feed->artist->relation-list as PHP interprets "list" as the list() function.

I have a feeling I'm going about this wrong (not much XML experience), and even if I was able to get hold of the elements I want, I don't know how to extract their attributes (I just want the type and target fields).

Can anyone gently nudge me in the right direction?

Thanks. Matt

+3  A: 

Have a look at the examples on the php.net page, they actually tell you how to solve this:

// $feed->artist->relation-list
$feed->artist->{'relation-list'}

To get an attribute of a node, just use the attribute name as array index on the node:

foreach( $feed->artist->{'relation-list'}->relation as $relation ) {
    $target = (string)$relation['target'];
    $type = (string)$relation['type'];
    // Do something with it
}

(Untested)

svens
Thank you, I seem to have missed that syntax. You're a hero!
Matt Andrews
Sometimes the SimpleXML syntax "feels" very weird, it has confused me a lot. Never forget to do the type-casting, or your script will blow up with strange errors. (:
svens