计算XML文件中的条目

时间:2011-09-09 02:03:50

标签: php xml curl simplexml

有没有办法计算给定xml文件中有多少条目?

示例:http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/rackemup420/cars?output=xml

我的代码:

// The POST URL and parameters      
$request =  'http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/'.$u.'/cars?output=xml';      

// Get the curl session object      
$session = curl_init($request);      

// Set the POST options.  
curl_setopt($session, CURLOPT_HEADER, true);      
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);      

// Do the POST and then close the session      
$response = curl_exec($session);      
curl_close($session);      

// Get HTTP Status code from the response      
$status_code = array();      
preg_match('/\d\d\d/', $response, $status_code);      

// Check for errors      
switch( $status_code[0] ) {      
    case 200:      
        // Success      
        break;      
    case 503:      
        die('Service unavailable. An internal problem prevented us from returning data to you.');      
        break;      
    case 403:      
        die('Forbidden. You do not have permission to access this resource, or are over your rate limit.');      
        break;      
    case 400:      
        // You may want to fall through here and read the specific XML error      
        die('Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');      
        break;      
    default:      
        die('Your call returned an unexpected HTTP status of:' . $status_code[0]);      
}      

// Get the XML from the response, bypassing the header      
if (!($xml = strstr($response, '<?xml'))) {      
    $xml = null;      
}      

// Output the XML      

$worldCar = simplexml_load_string($xml);     
foreach ($worldCar->worldCar as $cars)        
{    

    $playercarid = $cars['carId'];      
    $playercarmake = $cars['make'];      
    $playercarname = $cars['carName'];   

        $playercaraccel = $cars->physicsProfile['acceleration'];      
        $playercarhandle = $cars->physicsProfile['handling'];      
        $playercarrating = $cars->physicsProfile['rating'];  
        $playercarspeed = $cars->physicsProfile['topSpeed'];  
        $playercartier = $cars->physicsProfile['tier'];  
}  

2 个答案:

答案 0 :(得分:1)

获得计数

count( $worldCar->xpath('worldCar') );

循环(这是你的问题,你只得到第一个世界汽车)

foreach ($worldCar->xpath('worldCar') as $node)
{
  ...
}

或者

foreach ($worldCar->children() as $node)
{
  ..
}

答案 1 :(得分:0)

我认为你需要先加载文件: 查看PHP手册以获取有关加载XML文件的条目Here

然后,一旦您将文档加载到内存中,我相信您使用“XMLReader”对象来遍历节点并随时增加一个独立的计数器变量。

我相信this文章讨论了read-and-advance-to-next-node操作,尽管阅读了该文章底部的注释,如果你有一个非常大的XML文件,你可能会运行内存不足。请注意不要尝试解析1Tb文件或其他东西...... :)

祝你好运!

ħ

编辑:看起来你可以使用XMLReader对象来打开你想要阅读的文件,而且出于发布的目的,你可能想要使用XMLReader-&gt; next();

简单代码示例可能如下所示:

$nodeCount = 1;
$xr = XMLReader::open("myxmlfile.xml"); //Called staticly, returns an XMLReader object or false!
if(xr != false) // Check to see if its a valid object
{
   while($xr->next() == true) //Iterate through all the nodes
   {
      $nodeCount++; //increment an accumulator
   }
}
echo "$nodeCount nodes were found!";