PHP 中的 XML 循环语句

发布于 2024-11-04 04:36:42 字数 1363 浏览 0 评论 0原文

嘿,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-11-11 04:36:42

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

我想你有这样的事情:

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

自己测试一下,它有效。

Well, although the possible duplication here is a very simple example based on your file.

I suppose you have something like this:

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

In order to extract the summaries where the attribute type is "title", as you asked, you need something like this:

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

Test for yourself, it works.

掩于岁月 2024-11-11 04:36:42

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

$Attributes = $Node->attributes();

if you need to get attributes you can use belowe example

$Attributes = $Node->attributes();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文