【问题标题】:Count entries in a XML file对 XML 文件中的条目进行计数
【发布时间】:2011-09-09 02:03:49
【问题描述】:

有没有办法计算给定 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'];  
}  

【问题讨论】:

  • 您能解释一下您要计算的是哪个属性或元素吗?

标签: php xml curl simplexml


【解决方案1】:

获取计数

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

循环(这是你的问题,你只能得到第一辆 worldCar)

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

或者

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

【讨论】:

  • ty 就像一个魅力。我以前从未使用过 xml 的东西,我正在尝试深入研究。我将不得不做一些阅读:D
【解决方案2】:

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

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

我相信this 文章讨论了 read-and-advance-to-next-node 操作,尽管阅读该文章底部的 cmets,如果您有一个非常大的 XML 文件,您可能会用完记忆。请注意不要尝试解析 1Tb 文件或其他内容... :)

祝你好运!

H

编辑:看起来你可以使用 XMLReader 对象来打开你想要阅读的文件,而且看起来为了这篇文章的目的,你会想要使用 XMLReader->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!"; 

【讨论】:

  • 谢谢。我会调查的。
  • @rackemup420,欢迎您,只要确保您阅读了第二篇链接文章底部的 cmets 中的一些警告——显然,读者会将整个文件读入内存,如果您是处理一个非常大的文件可能会耗尽内存。
  • 不,我认为其中最多的条目是@ 30 ish。不要认为这会将它推出记忆。另一方面,我尝试了您发布的代码,它的计数为 2,而应该计数为 20。在那个 xml 文件中,我链接了我希望计算所有以 @987654324 @.
  • @rackemup420,那是因为next()只计算顶级节点,rtfm
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
相关资源
最近更新 更多