views:

28

answers:

2

Hi guys,

Quick newbie question here, how do I access totalResults?

XML

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"&gt;
  <opensearch:totalResults>1</opensearch:totalResults>
  <posts>
    <post>
      <score>10</score>
    </post>
  </posts>
</OpenSearchDescription>

To access the score I would do this:

PHP

$xmlObj = simplexml_load_string($theXMLabove);
echo $xmlObj->posts->post[0]->score;

But none of these work for the totalResults:

echo $xmlObj->opensearch:totalResults;
echo $xmlObj->opensearch->totalResults;

Sorry for asking such a lame question...

Documentation on how to traverse XML with PHP is also appreciated :)

Thanks!

A: 

try: $xmlObj->children('opensearch');

Im not sure if that will work though because from what you posted the opensearch namespace isnt defined as an xmlns. That might not make a difference though - im not sure because when ive had to deal with ns in simplexml the ns has always been explicitly defined.

prodigitalson
Since the namespace wasn't specified in the first place, this wasn't a well-formed XML document to begin with. Once the namespace is defined, though, the solution provided by thetaiko is the most appropriate.
EAMann
+1  A: 

with the namespace added you can do this:

$opensearch = $xmlObj->children('http://a9.com/-/spec/opensearch/1.1/');
echo $opensearch->totalResult;
thetaiko
Worked great, thanks :)
XaviEsteve