2011-09-09 26 views
1

有沒有一種方法可以計算給定xml文件中有多少個條目?在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']; 
} 
+0

您能否解釋您正在嘗試進行計數的屬性或元素? – ajreal

+0

'worldCar'元素 – rackemup420

回答

1

要獲得數

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

要循環(這是你的問題,你只得到第worldCar)

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

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

ty像一個魅力一樣工作。我從來沒有使用XML的東西,我試圖潛水在哈哈。我將不得不做一些閱讀:D – rackemup420

0

我認爲你需要首先加載該文件: 退房PHP手冊上加載XML的項目文件Here

然後一旦你將文檔加載到內存中,我相信你會使用一個「XMLReader」對象來遍歷節點,並隨着你的增加一個獨立的計數器變量。

我相信this文章討論了讀取和前進到下一個節點的操作,儘管閱讀那篇文章底部的註釋,如果你有一個非常大的XML文件,你可能會用盡內存。請注意,您不會嘗試解析1TB文件或其他內容... :)

祝您好運!

^h

編輯:看起來你可以使用XmlReader對象,以打開要讀取的文件,也它看起來像這個帖子的目的,您可能需要使用XMLReader->未來();

簡單的代碼示例可能是這樣的:

$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!"; 
+0

謝謝。我會研究它。 – rackemup420

+0

@ rackemup420,歡迎您,請確保您閱讀了第二篇鏈接文章底部的註釋中的一些警告 - 顯然讀者會將整個文件讀入內存,如果您正在處理的文件非常大可能會耗盡內存。 – hypervisor666

+0

不,我認爲一箇中最多的條目是@ 30 ish。不認爲這會推動它的內存。另一方面,我嘗試了你發佈的代碼,它的計數爲2,當應該有20的計數。在該XML文件我鏈接即時通訊期待計算所有以'worldCar開頭的條目'。 – rackemup420