Count entries in a XML file
Is there a way to count how many entries there are in a given xml file?
Example: http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/rackemup420/cars?output=xml
My Code:
// 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'];
}
To get count
count( $worldCar->xpath('worldCar') );
To loop (this is your problem, you only get the first worldCar)
foreach ($worldCar->xpath('worldCar') as $node)
{
...
}
Or
foreach ($worldCar->children() as $node)
{
..
}
I think you need to load the file first: Check out the PHP Manual for an entry on loading XML Files Here
Then once you have the document loaded into memory I believe you use an "XMLReader" object to walk the nodes and increment an independent counter variable as you go.
I believe this article discusses the read-and-advance-to-next-node operation, although read the comments at that bottom of that article, if you have a very large XML file, you could potentially run out of memory. Take care that you don't try and parse a 1Tb file or something... :)
Good luck!
H
Edit: It looks like you can use the XMLReader Object to open the file you want to read, and also it looks like for the purposes of this posting, you would want to use XMLReader->next();
Simple Code Example might look like this:
$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!";
精彩评论