johnslater Posted March 28, 2010 Share Posted March 28, 2010 I've had this issue before and someone created a new function that did the job, however that function doesn't work in this case which is a pain. I'm pulling an XML file and outputting the contents of the "album" tag and need to output each child element of album (name, playcount etc). I think the problem is with the album tag having a rank="1" attribute on it, so i can only output the first album, no further albums. Here is what i have so far that outputs album one... $artists_xml = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist='.$artist.'&api_key=b25b959554ed76058ac220b7b2e0a026'); $artists_array = $artists_xml->topalbums; //print_r($artists_array); foreach ($artists_array as $value) { //VARS $album_title = $value->album->name; return $album_title; } I want to be able to loop through all of the albums, not just the first and be able to drill into each one and get the child elements. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/196768-xml-attribute-problem/ Share on other sites More sharing options...
Mzor Posted March 28, 2010 Share Posted March 28, 2010 The link in that XML file leads to an XML file with an error message for me - would it be possible to paste the part of the file you're trying to get at? That'd make things easier as I don't know the structure of the XML file right now. Quote Link to comment https://forums.phpfreaks.com/topic/196768-xml-attribute-problem/#findComment-1033112 Share on other sites More sharing options...
salathe Posted March 28, 2010 Share Posted March 28, 2010 You're not referencing the list of albums correctly. To get that right, use $artists_xml->topalbums->album and then change how you access items inside the loop to something like $value->name. So to echo a list of album titles: $albums_array = $artists_xml->topalbums->album; foreach ($albums_array as $album) { $album_title = $album->name; echo $album_title . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/196768-xml-attribute-problem/#findComment-1033123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.