访问使用totalResults属性返回XML

问题描述:

我想从以下这是由谷歌返回的XML访问 '使用totalResults'(联系人)访问使用totalResults属性返回XML

<generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator> 
 
<openSearch:totalResults>29</openSearch:totalResults> 
 
<openSearch:startIndex>1</openSearch:startIndex> 
 
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>

$xml = '<generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator> 
<openSearch:totalResults>29</openSearch:totalResults> 
<openSearch:startIndex>1</openSearch:startIndex> 
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>' 

$xml = simplexml_load_string($xml); 
echo $xml->openSearch->totalResults; 
+0

对不起,它没有工作。如果你想看到[完整的原始XML文件](http://www.unifiedvu.com/xerotest/simple.xml) – Cheran

试试这个PHP来自PHP文档的教程how to access attributes in XML

<?php 
$string = <<<XML 
<a> 
<foo name="one" game="lonely">1</foo> 
</a> 
XML; 

$xml = simplexml_load_string($string); 
foreach($xml->foo[0]->attributes() as $a => $b) { 
    echo $a,'="',$b,"\"\n"; 
} 
?> 

上例将输出:

name="one" 
game="lonely" 

检查此SO post附加参考。