-2

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?

Max Rose-Collins
  • 1,904
  • 5
  • 26
  • 45
  • Your *breaks the code* description is a variation of *Does not work plz help*. I'm sure you can improve it. – Álvaro González Sep 09 '11 at 11:37
  • 1
    @Álvaro G. Vicario sorry, i don't understand what you just said? – Max Rose-Collins Sep 09 '11 at 11:42
  • I mean that you are expected to provide a clear description of the problem and ask a question, rather than drop a block of code and let us guess. Compare «breaks the code» with «triggers the following syntax error: "unexpected '@', expecting T_STRING or T_VARIABLE or '{' or '$'"» – Álvaro González Sep 09 '11 at 11:49
  • possible duplicate of [How to access element attributes with SimpleXml](http://stackoverflow.com/questions/4625045/how-to-access-element-attributes-with-simplexml/4625075#4625075) – Gordon Sep 09 '11 at 12:39
  • possible duplicate of ***Correction:* [Accessing @attribute from SimpleXML](http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml)** (prev: [what does means symbol "@" in this output?](http://stackoverflow.com/questions/7256396/what-does-means-symbol-in-this-output)) – hakre Jun 24 '13 at 01:17

3 Answers3

3

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

Community
  • 1
  • 1
ajreal
  • 46,720
  • 11
  • 89
  • 119
0

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

$entry->link->attributes()

Also there they can be accessed with [] operator - check next link http://www.impossible.co.in/blog/padya/accessuse-simplexmlelementattributes

Fivell
  • 11,829
  • 3
  • 61
  • 99
0

With SimpleXML, access to attributes is available through array-style syntax (or via attributes() as mentioned in other answers).

$entry->link['href'];

See: http://php.net/simplexml.examples-basic

salathe
  • 51,324
  • 12
  • 104
  • 132