为什么我使用非树XML解析器时会出现内存问题? (XML解析器)

时间:2011-07-22 13:36:53

标签: php xml parsing

我正在寻找使用XML解析器的方法,现在我尝试使用XML Parser。 (我也测试了XMLReader,但我发现它很慢)。

我读过它没有任何内存问题,因为它没有像DOM或SimpleXML那样将整个文档加载到内存中。

我将此代码用于测试目的,将$data = fread($fp, 4096);替换为$data = fread($fp, filesize($file));以加载整个文档并显示它,而不仅仅是少量文档。

当我这样做时,我收到此错误致命错误:允许的内存大小为67108864字节耗尽

有人能清楚我的想法并与我分享一些有关此事的知识吗?

XML文件是120mb。

<?php
// The XML file that you wish to be parsed
$file = "standard.xml";

// This function tells the parser what to do with the data once it reaches the contents
// that appear between tags.
function contents($parser, $data){
    echo $data;
}

// This function tells the parser to place a <b> where it finds a start tag.
function startTag($parser, $data){
    echo "<b>";
}

// And this function tells the parser to replace the end tags with "<b><br />"
function endTag($parser, $data){
    echo "</b><br />";
}

// These lines create the parser and then set the functions for the parser to use when
// reading the document.
$xml_parser = xml_parser_create();

// Sets the functions for start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");
// Sets the function for the contents/data
xml_set_character_data_handler($xml_parser, "contents");

// Opens the file for reading
$fp = fopen($file, "r");

// Read the file and save its contents as the variable "data"
$data = fread($fp, filesize($file));

// This if statement does two things. 1) it parses the document according to our 
// functions created above. 2) If the parse fails for some reason it returns an
// error message and also tells us which line the error occured at.
if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

// Free the memory used to create the parser
xml_parser_free($xml_parser);

// Close the file when you're done reading it
fclose($fp);
?>

1 个答案:

答案 0 :(得分:1)

$data = fread($fp, filesize($file));会将整个文件读入内存。如果你有64MB的内存,文件的大小是120MB,那么我想你可以看到为什么会失败。

重新阅读xml_parse的文档,并使用较小的块来提供数据,以便在内存限制内工作:)