2011-09-09 6 views
1

指定されたxmlファイルに含まれるエントリの数をカウントする方法はありますか?XMLファイル内のエントリ数をカウントする

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

マイコード:次にロードXML上のエントリのHere

ファイルをPHPマニュアルをチェックアウト :私はあなたが最初のファイルをロードする必要があると思う

// 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')); 

をカウントする

foreach ($worldCar->xpath('worldCar') as $node) 
{ 
    ... 
} 
(これはあなたの問題がある、あなただけの最初worldCarを取得します)

または

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

がチャームのように機能していたためです。私は前にXMLのものを使用したことがないと私は笑いでダイビングしようとしています。私はいくつかの読書をしなければならないだろう:D – rackemup420

0

ドキュメントをメモリにロードすると、XMLReaderオブジェクトを使用してノードを歩き、独立したカウンタ変数をインクリメントすると思います。

私は非常に大きなXMLファイルを持っている場合、その記事の一番下にあるコメントを読んでいますが、次のノードへのread-and-advance-node操作について説明しています。 。 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、ようこそ、2番目のリンク先の記事の下部にあるコメントの警告を読んでください。読者はファイル全体をメモリに読み込みます。非常に大きなファイルを扱っている場合は、メモリが足りなくなる可能性があります。 – hypervisor666

+0

nah私は1つのほとんどのエントリは@ 30 ishだと思う。それはそれを記憶から追い出すとは思わない。一方、あなたが投稿したコードを試してみました。そして、カウントが20になるはずの2のカウントが表示されます。そのxmlファイルでは、worldCarで始まるすべてのエントリを数えたいと思っていました'。 – rackemup420

関連する問題