我正在将80 MB XML文件转换为Array(),在处理过程中,它需要几乎1 GB的RAM。这是正常的吗?我的意思是我尝试提高资源效率并使用逐行读取文件的xml_parser,但1 GB对我来说真的很惊讶。
以下是代码:
class XmlToArray
{
protected $_stack = array();
protected $_file = "";
protected $_parser = null;
protected $_root = array();
public function __construct($file)
{
$this->_file = $file;
$this->_parser = xml_parser_create("UTF-8");
xml_set_object($this->_parser, $this);
xml_set_element_handler($this->_parser, "startTag", "endTag");
}
public function startTag($parser, $name, $attribs)
{
$new_node = array('name' => strtolower($name), 'attr' => $attribs, 'sub' => array());
$stack = $this->_stack;
$current = &$stack[count($stack) - 1];
if (is_array($current))
{
$current['s'][] = &$new_node;
}
else
{
$this->_root = &$new_node;
}
$this->_stack[] = &$new_node;
}
public function endTag($parser, $name)
{
array_pop($this->_stack);
}
public function convert()
{
$fh = fopen($this->_file, "r");
if (!$fh)
{
throw new Exception("fail");
}
while (!feof($fh))
{
$data = fread($fh, 4096);
xml_parse($this->_parser, $data, feof($fh));
}
return $this->_root;
}
}
答案 0 :(得分:1)
可悲的是,这并不罕见。与XML的结构有关。具有大量复杂细节的阵列最终会变得非常强大。 10X文件的大小不是那么异常。你真的需要一次装满它吗?
(OP说我应该将此作为答案发布)