简体   繁体   English

php simplexml_load_file带短划线( - )

[英]php simplexml_load_file with a dash ( - )

Hi I'm struggling to get data from an xml file in php... 嗨,我很难从php中的xml文件中获取数据...

the xml file is here: http://musicbrainz.org/ws/2/artist/8bfac288-ccc5-448d-9573-c33ea2aa5c30?inc=release-groups xml文件在这里: 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: 在将其作为$ xml加载到simplexml_load_file之后到目前为止,我想做类似这样的事情:

<?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. 当我尝试在带有$ i变量的for循环中使用它时会出现问题,如上所示。

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). 这个脚本适用于我,下面的更改(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 你还没有告诉我们问题是什么,但我猜你没有阅读PHP手册中SimpleXML部分 ,它说的是

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. 访问包含PHP命名约定下不允许的字符的XML文档中的元素(例如连字符)可以通过将元素名称封装在大括号和撇号中来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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