|
|
 |
Re: FN-FORUM: php xml_parser
date posted 15th April 2008 14:19
On 15/04/2008 15:34, Steven wrote:
>
> Hi Guys,
>
> I'm really pulling my hair out here. I am using the following class to
> parse a number of xml files that I am retrieving from a remote server.
> Each xml file has 1000 row tags E.G. ... etc. I can parse the
> first xml file fine, no problems however when I try to get the next xml
> file. I get the following error (I have set php.ini to use 48MB of memory).
>
> *Fatal error*: Allowed memory size of 50331648 bytes exhausted (tried to
> allocate 32 bytes) in
> */var/www/rainbow/public_html/php/classes/merlin.class.php* on line *139
>
> in each loop i use the following code.
>
> while(glob("*.xml") as $filename) {
> $xml_data = $this->getXmlData($filename);
> $xml_parser = new xml();
> $xml_parser->parse($xml_data);
> $dom = $xml_parser->dom;
> * }
>
> If I change the code and move the * $xml_parser = new xml(); line to
> before the while loop, I do not get the fatal error, however the
> contents of $dom remain as the previous loop xml contents.
>
> Does any one have any ideas to over come this?
>
> Steve
> *
Could you set $dom to null at the start of the while loop?
Increase the amount of memory available in the php ini?
Try set the memory available dynamically in the code based on the xml
file size - I do something similar when dealing with jpegs, I'm sure it
could be adapted (the original code probably came from php.net):
private static function setMemoryForImage( $filename )
{
$imageInfo = getimagesize($filename);
$MB = 1048576; // number of bytes in 1M
$K64 = 65536; // number of bytes in 64K
$TWEAKFACTOR = 16; // Or whatever works for you
$memoryNeeded = round( ( $imageInfo[0] * $imageInfo[1]
* $imageInfo['bits']
* $imageInfo['channels'] / 8
+ $K64
) * $TWEAKFACTOR
);
//ini_get('memory_limit') only works if compiled with
"--enable-memory-limit" also
//Default memory limit is 8MB so well stick with that.
//To find out what yours is, view your php.ini file.
$memoryLimit = 8 * $MB;
if (function_exists('memory_get_usage') &&
memory_get_usage() + $memoryNeeded > $memoryLimit)
{
$newLimit = $memoryLimitMB + ceil( ( memory_get_usage()
+ $memoryNeeded
- $memoryLimit
) / $MB
);
ini_set( 'memory_limit', $newLimit . 'M' );
return true;
}
else
{
return false;
}
}
Martin
|
 |
|