【问题标题】:XML Loop Statement in PHPPHP 中的 XML 循环语句
【发布时间】: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


【解决方案1】:

好吧,虽然这里可能的重复是基于您的文件的一个非常简单的示例。

我想你有这样的事情:

<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>

为了提取属性 type 为“title”的摘要,正如您所要求的,您需要这样的东西:

<?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";
    }
?>

自己测试一下,效果不错。

【讨论】:

  • 嗨,nunaxe,您的答案似乎是正确的,我试图实现您的代码。我用完整的 php 代码编辑了我的问题。我正在使用 cURL,但它返回错误:致命错误:未捕获的异常 'Exception' 带有消息'字符串无法在您将 xml 加载到变量 xmlmusic 的行上被解析为 XML
  • 这意味着 simplexml 在解析 XML 时遇到问题。您可以尝试输出 $result 并验证它是有效的 XML 吗?
  • 听从 DaOgre 的建议。做print_r($result)。在我的系统中,由于禁止入站信息检索,对 cURL 和 simplexml_load_file() 的调用无法按预期工作。
  • 它打印出 xml(我相信它的 xml)一个提取物 -- DJ Tiësto discogs.com/artist/DJ+Ti%C3%ABsto</uri></… end="20" numResults="3279" start="1">DJ Tiesto discogs.com/artist/DJ+Tiesto</uri><summary
  • 我用一个有效的解决方案编辑了我的答案。它使用 file_get_contents() 代替 cURL(它更简单)并且不要忘记将 SimpleXMLElement() 的最后一个参数更改为 false .
【解决方案2】:

如果你需要获取属性,你可以使用下面的例子

$Attributes = $Node-&gt;attributes();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2011-05-08
    • 2017-06-08
    相关资源
    最近更新 更多