Simple XML question

Hello forums

I have this xml:

<entry>
<id>1256</id>
 <yt:firstName>Disturbed</yt:firstName>
 <yt:gender>m</yt:gender>
 <yt:hometown>Chicago, Illinois</yt:hometown>
 <yt:location>Chicago, Illinois, US</yt:location>
</entry>

How can I get the items inside the yt tag using simple xml

here’s my code

$feed ='http://gdata.youtube.com/feeds/api/users/DisturbedTV';
$sxml = simplexml_load_file($feed);

echo $sxml->id // echos 1256
echo $sxml->yt->firstName //nothing 

Thanx

yt isnt a container, it’s a namespace. A quick Sitepoint Googlefu says: http://www.sitepoint.com/simplexml-and-namespaces/

As StarLion pointed out, the yt is not the element name but rather the namespace to which those elements belong. Have a look around more generally for what namespaces in XML are all about, it’ll do you good.

With SimpleXML, you can easily work with those prefixes (or the full URI as the old Sitepoint article above shows) by using the children() method:


$feed ='http://gdata.youtube.com/feeds/api/users/DisturbedTV';
$sxml = simplexml_load_file($feed);
echo $sxml->children('yt', TRUE)->firstName; // Disturbed