循环 SimpleXMLElement 对象只需 2 步

发布于 2024-09-29 14:06:25 字数 2305 浏览 0 评论 0原文

我有一个 SimpleXMLElement,我使用curl 在 XML 文件中读取它。 尝试从中获取值时,我似乎需要使用嵌套的 foreach,但在类似的情况下,我能够一次性完成此操作。现在我真的很好奇为什么这不起作用:

它是美味的 RSS feed;

SimpleXMLElement Object
(
    [title] => Delicious/chrisvdberge
    [link] => http://www.delicious.com/chrisvdberge
    [description] => bookmarks posted by chrisvdberge
    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [title] => Diagrammr
                    [pubDate] => Thu, 06 May 2010 12:40:07 +0000
                    [guid] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771#chrisvdberge
                    [link] => http://www.diagrammr.com/
                    [comments] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771
                    [source] => chrisvdberge's bookmarks
                    [category] => Array
                        (
                            [0] => tagthis
                            [1] => visualization
                            [2] => diagrams
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [title] => Ignite Toronto 2: Ryan Coleman - Designing for visual efficiency on Vimeo
                    [pubDate] => Mon, 18 Jan 2010 08:05:06 +0000
                    [guid] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537#chrisvdberge
                    [link] => http://vimeo.com/8317770
                    [comments] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537
                    [source] => chrisvdberge's bookmarks
                    [category] => Array
                        (
                            [0] => tagthis
                            [1] => vizthink
                        )

                )

我可以使用嵌套的 foreach 访问所有链接:

foreach ($delicious_res as $item) {
    foreach ($item->item as $entry){
        echo $entry->link."<br />";
    }
}

我不明白为什么我不能使用这个(在另一种情况下我可以)

foreach ($delicious_res->item as $item) {
        echo $entry->link."<br />";
}

我真的很想了解为什么,以便更好地理解 SimpleXML ;)

I've got a SimpleXMLElement for which I read in a XML file using curl.
Trying to get the values out of it I seem to need to use a nested foreach, but in a similar situation I was able to do this in one go. Now I'm really curious why this isn't working:

Its the delicious RSS feed;

SimpleXMLElement Object
(
    [title] => Delicious/chrisvdberge
    [link] => http://www.delicious.com/chrisvdberge
    [description] => bookmarks posted by chrisvdberge
    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [title] => Diagrammr
                    [pubDate] => Thu, 06 May 2010 12:40:07 +0000
                    [guid] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771#chrisvdberge
                    [link] => http://www.diagrammr.com/
                    [comments] => http://www.delicious.com/url/2fc2ed0e870ec9d001b645dfaed0e771
                    [source] => chrisvdberge's bookmarks
                    [category] => Array
                        (
                            [0] => tagthis
                            [1] => visualization
                            [2] => diagrams
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [title] => Ignite Toronto 2: Ryan Coleman - Designing for visual efficiency on Vimeo
                    [pubDate] => Mon, 18 Jan 2010 08:05:06 +0000
                    [guid] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537#chrisvdberge
                    [link] => http://vimeo.com/8317770
                    [comments] => http://www.delicious.com/url/9a0de307f3f37baaa25ad20b4dbc4537
                    [source] => chrisvdberge's bookmarks
                    [category] => Array
                        (
                            [0] => tagthis
                            [1] => vizthink
                        )

                )

etc.

I can access all the links by using a nested foreach:

foreach ($delicious_res as $item) {
    foreach ($item->item as $entry){
        echo $entry->link."<br />";
    }
}

But I don't understand why I cant use this (which I can in another situation)

foreach ($delicious_res->item as $item) {
        echo $entry->link."<br />";
}

I really like to understand why in order to get a better understanding of SimpleXML in general ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏九 2024-10-06 14:06:25

您必须使用:

foreach($delicious_res->channel->item as $item) {
    print_r($item);
}

因为项目集合位于 标记内(标签可以省略,因为它是文档的根节点)。

另请注意:如果您没有做任何花哨的事情,您可能不需要使用 curl 来获取 rss feeds xml 内容。只需使用 simplexml_load_file 并将 url 作为参数:

$delicious_res = simplexml_load_file('http://path-to-your/file.xml"');

You have to use:

foreach($delicious_res->channel->item as $item) {
    print_r($item);
}

Because the collection of items is inside the <rss> and <channel> tag (the <rss> tag can be omitted as it is the root node of the document).

On another note: if you are not doing anything fancy, you probably don´t need to use curl to fetch the rss feeds xml content. Just use simplexml_load_file with the url as an argument:

$delicious_res = simplexml_load_file('http://path-to-your/file.xml"');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文