ZaneDeFazio
ZaneDeFazio

Reputation: 509

PHP: calling a child tag simplexml

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

Upvotes: 3

Views: 662

Answers (2)

Chris
Chris

Reputation: 10435

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)

Upvotes: 4

Geoff Adams
Geoff Adams

Reputation: 1119

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.

Upvotes: 0

Related Questions