simplexml_load_file parse [@attributes] =>排列

时间:2011-09-09 11:18:46

标签: php xml parsing simplexml

我正在尝试解析此xml提要

[0] => SimpleXMLElement Object
            (
                [title] => Johannesburg in November
                [link] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [rel] => alternate
                                [type] => text/html
                                [href] => http://www.tompeters.com/dispatches/012120.php?rss=1
                            )

                    )

                [id] => tag:www.tompeters.com,2011://2.12120
                [published] => 2011-09-08T14:03:23Z
                [updated] => 2011-09-08T14:11:49Z
                [summary] => Tom will be giving a day-long presentation in November in Johannesburg, South Africa. Our friends at the Business Results Group...
                [author] => SimpleXMLElement Object
                    (
                        [name] => Shelley Dolley
                    )

                [category] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [term] => Announcements
                                [scheme] => http://www.sixapart.com/ns/types#category
                            )

                    )

                [content] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [type] => html
                            )

                    )

            )

我的PHP代码是

$url = 'http://www.tompeters.com/atom.xml';
                $xml = simplexml_load_file($url);

                echo '<pre>';
                foreach($xml->entry as $entry){
                    echo $entry->title;
                    echo "<br />";
                    foreach ($entry->link->@attributes as $attr){
                        echo $attr->href;
                        echo "<br />";
                    }
                }

问题是@attributes位破坏了代码..

我如何获得该href链接?

3 个答案:

答案 0 :(得分:3)

The @ = attributes,所以

foreach ($entry->link->attributes() ...

Official documentation

您似乎对SimpleXML有疑问(基于您过去的问题),
为了更好地理解,可能需要阅读SimplXML文档。

SO标签 - https://stackoverflow.com/questions/tagged/simplexml?sort=votes

答案 1 :(得分:0)

http://php.net/manual/en/simplexmlelement.attributes.php

$entry->link->attributes()

还可以使用[]运算符访问它们 - 检查下一个链接 http://www.impossible.co.in/blog/padya/accessuse-simplexmlelementattributes

答案 2 :(得分:0)

使用SimpleXML,可以通过数组样式语法(或通过其他答案中提到的attributes())访问属性。

$entry->link['href'];

请参阅:http://php.net/simplexml.examples-basic

相关问题