PHP中的XML循环语句
嘿,我有一个simpleXMLElement文档,它有一个与艺术家相册相关的数据数组 下面是该XML的摘录PHP中的XML循环语句,php,xml,parsing,simplexml,echo,Php,Xml,Parsing,Simplexml,Echo,嘿,我有一个simpleXMLElement文档,它有一个与艺术家相册相关的数据数组 下面是该XML的摘录 [2] => SimpleXMLElement Object ( [@attributes] => Array ( [num] => 3
[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";
}
attributes();
如果($attrs[“type”]=“title”)
echo$xm->摘要。“\n”;
}
?>如果需要获取属性,可以使用下面的示例
$Attributes=$Node->Attributes()代码>好吧,尽管这里可能的重复是基于您的文件的一个非常简单的示例
我想你有这样的东西:
<music>
<dj num="3" type="artist">
<title>DJ Tiesto</title>
<uri>http://www.discogs.com/artist/DJ+Ti%C3%ABsto</uri>
<summary>DJ Tiësto Tijs Michiel Verwest Dutch trance DJ producer.</summary>
</dj>
<dj num="4" type="artist">
<title>title</title>
<uri>url</uri>
<summary>summary</summary>
</dj>
<?php
$result = file_get_contents("http://www.discogs.com/search?type=all&" .
"q=DJ+Tiësto&" .
"f=xml&" .
"api_key=<key>");
$xmlmusic = new SimpleXMLElement($result, NULL, false);
//print_r($xmlmusic);
foreach ($xmlmusic as $xm)
{
$attrs = $xm->attributes();
if($attrs["type"] == "title")
echo $xm->summary."\n";
}
?>
铁斯托
http://www.discogs.com/artist/DJ+Ti%C3%abso
DJ Tiësto Tijs Michiel Verwest荷兰恍惚DJ制作人。
标题
网址
总结
如您所问,为了提取属性类型为“title”的摘要,您需要如下内容:
<music>
<dj num="3" type="artist">
<title>DJ Tiesto</title>
<uri>http://www.discogs.com/artist/DJ+Ti%C3%ABsto</uri>
<summary>DJ Tiësto Tijs Michiel Verwest Dutch trance DJ producer.</summary>
</dj>
<dj num="4" type="artist">
<title>title</title>
<uri>url</uri>
<summary>summary</summary>
</dj>
<?php
$result = file_get_contents("http://www.discogs.com/search?type=all&" .
"q=DJ+Tiësto&" .
"f=xml&" .
"api_key=<key>");
$xmlmusic = new SimpleXMLElement($result, NULL, false);
//print_r($xmlmusic);
foreach ($xmlmusic as $xm)
{
$attrs = $xm->attributes();
if($attrs["type"] == "title")
echo $xm->summary."\n";
}
?>
attributes();
如果($attrs[“type”]=“title”)
echo$xm->摘要。“\n”;
}
?>
你应该先试试,这样可以节省一些时间。可能重复的可能重复的可能重复的Hi nunax你的答案似乎是对的,我试着实现你的代码。我用完整的php代码编辑了我的问题。Im使用cURL,但它返回一个错误:致命错误:未捕获异常“exception”,消息为“String无法在将XML加载到变量xmlmusict的行上解析为XML”,这意味着simplexml在解析XML时遇到问题。您可以尝试输出$result并验证它是有效的XML吗?请遵循DaOgre的建议。执行打印($result)
。在我的系统中,由于禁止入站信息检索,对cURL和simplexml_load_file()的调用没有按预期工作。它打印xml(我相信它的xml)一个摘录——DJ Tiëstodiscogs.com/artist/DJ+Ti%C3%ABstoDJ Tiestodiscogs.com/artist/DJ+TiestoI用一个有效的解决方案编辑了我的答案。它使用file_get_contents()(简单得多),而不是cURL,并且不要忘记将simplexmlement()的最后一个参数更改为false。