Mr. Engineer
Mr. Engineer

Reputation: 3515

Access xml object

I'm using YouTube API. I got following result from API :

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => alternate
            [href] => http://www.youtube.com/watch?v=blabla
        )

)

I'm confuse with this object. I want to access @attributes. How can i do it?

Upvotes: 3

Views: 52

Answers (1)

VolkerK
VolkerK

Reputation: 96159

The @attributes part of the print_r output are just the element's attributes which can be accessed via $obj['attrname'].

<?php
$obj = new SimpleXMLElement('<foo rel="alternate" href="http://www.youtube.com/watch?v=blabla" />');
print_r($obj); // to verify that the sample data fits your actual data

echo $obj['rel'], ' | ', $obj['href'];

prints

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => alternate
            [href] => http://www.youtube.com/watch?v=blabla
        )

)
alternate | http://www.youtube.com/watch?v=blabla

see also Example #5 Using attributes in the SimpleXML documentation.

Upvotes: 2

Related Questions