1

I have been tasked with parsing data returned from one of our providers. The XML has two attribute arrays and I need to retrieve information from the second. Following is the data I am using as displayed by print_r.

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => Content Mix for Weather-in-Pictures
            [link] => http://www.abc123.com/
            [description] => abc123 Content Server
            [docs] => http://www.rssboard.org/rss-specification
            [generator] => abc123 Content Server
            [lastBuildDate] => Wed, 22 Apr 2015 19:36:51 GMT
            [ttl] => 5
            [item] => SimpleXMLElement Object
                (
                    [title] => Weather in Pictures
                    [guid] => mk1pL9MkC2f3tz71QgUwyA2
                    [pubDate] => Wed, 22 Apr 2015 19:36:51 GMT
                    [enclosure] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [length] => 204800
                                    [type] => image/jpeg
                                    [url] => http://abc123.com
                                )
                        )
                )
        )
)

The url is what I need to retrieve. I have a feeling this is going to be one of those "DUH!" moments when someone points out what I need to do.

current ($xml(attributes)) // returns only version.
$xml->attributes()->url; // does not return anything.
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jay Hall
  • 11
  • 1
  • 1
    "The XML has two attribute arrays" No, it doesn't. There is no such thing as an array in XML, and the word "array" in the `print_r` output of SimpleXML is just there to suggest how you access it. It's not clear from your question what `$xml` is pointing at - is it the whole document you show above? If so, you need to navigate to the specific element you want to look at - it can't know just from "attributes" that you want to look at the first `` of the first ``. – IMSoP Apr 23 '15 at 11:18
  • Thank you very much. That explained exactly what I need to do. – Jay Hall Apr 23 '15 at 12:10

0 Answers0