【发布时间】:2011-08-15 18:27:28
【问题描述】:
嘿,我有一个 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";
}
?>
【问题讨论】:
-
你应该先尝试一下,节省一些时间。
标签: php xml parsing simplexml echo