Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

php - simplexml_load_file parse [@attributes] => Array

I am trying to parse this xml feed

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

                    )

            )

my PHP code to do so is

$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 />";
                    }
                }

the problem is that the @attributes bit breaks the code..

How do i get that href link?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The @ = attributes, so

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

Official documentation

It seems you have some doubts over SimpleXML (based on your past question),
it might worth to read the SimplXML documentation for better understanding.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...