1

Hi I'm struggling to get data from an xml file in php...

the xml file is here: http://musicbrainz.org/ws/2/artist/8bfac288-ccc5-448d-9573-c33ea2aa5c30?inc=release-groups

and so far after loading it into simplexml_load_file as $xml I want to do something like this:

<?php
$url = "http://musicbrainz.org/ws/2/artist/8bfac288-ccc5-448d-9573-c33ea2aa5c30?inc=release-groups";
$xml = simplexml_load_file($url);

$releasegrouplist = "release-group-list"; 
$releasegroup = "release-group";

$i = "0";
for ($i = 0; $i <= 30; $i++)
  {
   echo "release: " . $xml->artist->$releasegrouplist->$releasegroup[$i]->title;}

The problem arises when I try to use it in a for loop with an $i variable as shown above.

Any tips or easier ways to do this?

Thanks everyone

zrajm
  • 1,361
  • 1
  • 12
  • 21
d-_-b
  • 21,536
  • 40
  • 150
  • 256

2 Answers2

3

Try this syntax instead. This script works for me with the change below (PHP 5.3.6).

echo "release: " . $xml->artist->{'release-group-list'}->{'release-group'}[$i]->title;

Source

Community
  • 1
  • 1
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
0

You haven't actually told us what the problem is, but I'm guessing it's that you haven't read the SimpleXML section in the PHP manual, where it says

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Colin Fine
  • 3,334
  • 1
  • 20
  • 32