tags:

views:

19

answers:

2

Trying to parse a YouTube feed using PHP

simplexml_load_file();

I am able to access other tags pretty simply using

$xml->id;

When I try to access

<openSearch:totalResults>

with

$xml->openSearch->totalResults;

I don't get any results

A: 

Those elements are in a different XML namespace, to obtain them you need to do:

$xml->children('openSearch', true);

Then in the collection that is returned, you will find the elements you need.

Geoff Adams
+2  A: 

openSearch is a namespace - it's not the name of the tag, or a parent, or anything like that. Somewhere in the document there will be an attribute called xmlns:openSearch which defines the openSearch namespace (with an URL).

You can use the children method to get children of a certain namespace, and do something like:

$xml->children('openSearch', true)->totalResults

(You can also use the full URL for the namespace instead of 'openSearch' and leave the true off of the end, which may be beneficial if they ever change their markup or you parse similar feeds from elsewhere which use a different namespace prefix)

Chris Smith