开发者

Accessing XML/PHP with period in tag

开发者 https://www.devze.com 2022-12-30 14:54 出处:网络
Quick newbie question here, how do I access totalResults? XML <?xml version=\"1.0\" encoding=\"UTF-8\"?>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号