Accessing XML/PHP with period in tag
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/">
<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(开发者_JS百科$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!
with the namespace added you can do this:
$opensearch = $xmlObj->children('http://a9.com/-/spec/opensearch/1.1/');
echo $opensearch->totalResult;
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.
精彩评论