I've written a small function to read an XML file with simpleXML and used Pear Benchmark Iterate to time it. I've read that simpleXML and DOM puts the entire XML file into memory which can create a lot of overhead on files of a large size.
我编写了一个小函数,用simpleXML读取XML文件,并使用Pear Benchmark Iterate计时。我已经读过simpleXML和DOM将整个XML文件放入内存中,这会对大型文件产生大量开销。
The function below is a relatively small size and I'm only pulling 10 values from the XML file.
下面的函数是一个相对较小的大小,我只从XML文件中提取10个值。
Iterations of the function below (xml filesize: 200kb) takes 2.5 seconds to complete on average.
以下函数的迭代(xml文件大小:200kb)平均需要2.5秒才能完成。
Can anyone suggest a solution with PHP XMLparser, a related class, or perhaps some other efficient way to parse xml into an array?
任何人都可以使用PHP XMLparser建议一个解决方案,一个相关的类,或者可能是一些其他有效的方法来解析xml到一个数组?
SimpleXML version
SimpleXML版本
function getItems($file_id, $item_count=5)
{
switch ($file_id)
{
case '1':
$file = "http://xml_file.xml";
if ($xml = simplexml_load_file($file))
{
$i=0;
foreach ($xml->info as $info)
{
if ($i < $item_count)
{
$var[] = array(
"id" => (string)$info->id,
"name" => (string)$info->name);
}
$i++;
}
return $var;
}
}
}
1 个解决方案
#1
1
The XML Parser module may be the better option that doesn't require some external library, although it requires a bit of a mind shift. They payback is that the data is never loaded into memory in its entirety.
XML Parser模块可能是更好的选择,不需要一些外部库,虽然它需要一点思维转变。他们的回报是数据永远不会完整地加载到内存中。
In a nutshell, you will need to treat the XML document in a tree descending matter and keep a journal of your location in the tree as opening and closing tags are reported to you through handlers.
简而言之,您将需要以树降序处理XML文档,并在树中保留您的位置日志,因为通过处理程序向您报告打开和关闭标记。
It may seem daunting that this module is not OO, but xml_set_object alleviates this.
这个模块不是OO似乎令人生畏,但xml_set_object缓解了这个问题。
#1
1
The XML Parser module may be the better option that doesn't require some external library, although it requires a bit of a mind shift. They payback is that the data is never loaded into memory in its entirety.
XML Parser模块可能是更好的选择,不需要一些外部库,虽然它需要一点思维转变。他们的回报是数据永远不会完整地加载到内存中。
In a nutshell, you will need to treat the XML document in a tree descending matter and keep a journal of your location in the tree as opening and closing tags are reported to you through handlers.
简而言之,您将需要以树降序处理XML文档,并在树中保留您的位置日志,因为通过处理程序向您报告打开和关闭标记。
It may seem daunting that this module is not OO, but xml_set_object alleviates this.
这个模块不是OO似乎令人生畏,但xml_set_object缓解了这个问题。