PHP 中的 XML 循环语句
嘿,我有一个 simpleXMLElement 文档,其中包含与艺术家专辑有关的数据数组。
下面是该 XML 的摘录。
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[num] => 3
[type] => artist
)
[title] => DJ Tiësto
[uri] => http://www.discogs.com/artist/DJ+Ti%C3%ABsto
[summary] => DJ Tiësto Tijs Michiel Verwest Dutch trance DJ & producer.
在此 XML 文档中,有多种不同类型的信息,从艺术家信息到专辑标题。我想提取该数据的某些部分并回显它们。例如,我想提取将 [type] 定义为艺术家的数组条目的摘要。我猜我会使用一个 foreach 循环来遍历所有条目并检查这是否属实?这是正确的做法吗?
我为我令人困惑的解释道歉
---- 编辑 ----
这是获取数据的 PHP 代码 -
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.discogs.com/search?type=all&" .
"q=DJ+Tiësto&" .
"f=xml&" .
"api_key=<key>");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
$result = curl_exec($curl);
curl_close($curl);
$xmlmusic = new SimpleXMLElement($result,NULL,true);
foreach ($xmlmusic as $xm)
{
$attrs = $xm->attributes();
if($attrs["type"] == "title")
echo $xm->summary."\n";
}
?>
Hey, i have an simpleXMLElement document that features an array of data to do with an artist's albums.
Below is an extract of that XML
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[num] => 3
[type] => artist
)
[title] => DJ Tiësto
[uri] => http://www.discogs.com/artist/DJ+Ti%C3%ABsto
[summary] => DJ Tiësto Tijs Michiel Verwest Dutch trance DJ & producer.
In this XML document there are multiple different types of information from artist info to titles of albums. I want to extract certain parts of this data and echo them out. For instance i want to extract the summary's of the array entries that have the [type] defined to artist. I'm guessing i would use a foreach loop that went through all the entries and checked if this was true? Is this the right way to go about it.
I apologise for my confusing explanation
---- EDIT ----
Heres the PHP code that grabs the data -
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.discogs.com/search?type=all&" .
"q=DJ+Tiësto&" .
"f=xml&" .
"api_key=<key>");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_ENCODING, "gzip");
$result = curl_exec($curl);
curl_close($curl);
$xmlmusic = new SimpleXMLElement($result,NULL,true);
foreach ($xmlmusic as $xm)
{
$attrs = $xm->attributes();
if($attrs["type"] == "title")
echo $xm->summary."\n";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,虽然这里可能的重复是基于您的文件的一个非常简单的示例。
我想你有这样的事情:
为了提取属性 type 为“title”的摘要,正如您所要求的,您需要这样的东西:
自己测试一下,它有效。
Well, although the possible duplication here is a very simple example based on your file.
I suppose you have something like this:
In order to extract the summaries where the attribute type is "title", as you asked, you need something like this:
Test for yourself, it works.
如果您需要获取属性,可以使用下面的示例
$Attributes = $Node->attributes();
if you need to get attributes you can use belowe example
$Attributes = $Node->attributes();