简体   繁体   中英

php simplexml_load_file with a dash ( - )

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

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

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 (eg the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM