Converting XML to Array eats a lot of memory
I am converting 80 MB XML file into an Array() and during process it takes almost 1 GB of RAM. Is it normal? I mean I try to be resource efficient and use xml_parser which reads file line by line, but 1 GB is really surprise for me.
Here is the code:
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' => $attrib开发者_如何学Cs, '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;
}
}
Sadly, this is not uncommon. Has to do with the structure of the XML. Arrays with lots of complex details can end up pretty beefy. 10X the size of the file isn't that abnormal. Do you really need to load it all up in one shot?
(OP said I should post this as the answer)
精彩评论