Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access xml object

Tags:

php

xml

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?

like image 421
Mr. Engineer Avatar asked Sep 28 '26 20:09

Mr. Engineer


1 Answers

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.

like image 180
VolkerK Avatar answered Oct 01 '26 14:10

VolkerK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!