Bug #17114 Updated: xml_parse_into_struct takes too long, sometimes crashes IIS
| From: | bbisgod at hotmail dot com | Date: | Thu, 09 May 2002 12:03:06 +0000 |
| Subject: | Bug #17114 Updated: xml_parse_into_struct takes too long, sometimes crashes IIS | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-7314@lists.php.net to get a copy of this message | ||
ID: 17114
Updated by: bbisgod@hotmail.com
Reported By: bbisgod@hotmail.com
Status: Open
Bug Type: *XML functions
Operating System: Windows NT4
PHP Version: 4.2.0
New Comment:
apologies, I withdraw my bug report.
The problem was 2 things
a) script time outs
b) invalid XML
Previous Comments:
------------------------------------------------------------------------
[2002-05-09 07:24:46] bbisgod@hotmail.com
Running PHP4.2.0 on IIS on NT4 server.
I am trying to parse large XML files with PHP. When I run the code on
a smaller 66kb file, it runs with the desired affects. But when I try
to run it on a large file (around 2mb), it takes its time doing
xml_parse_into_struct. Also, sometimes the PHP script can make the
memory usage of IIS spiral uncontrollably, and the CPU usage is maxed
out.
Please help, or tell me whats wrong
Thanks in advance
INDEX.PHP
---------
<?
include("func.php");
$time = getmicrotime();
?>
<HTML>
<BODY>
<FORM method=post action="index.php" enctype="multipart/form-data">
<INPUT type=file name="XML"><BR>
<SELECT size=10 name="folderfile">
<?
$handle=opendir("../xml/");
while ($file = readdir($handle)) {
if ($file != "." && $file != "..") {
echo "<option>$file";
}
}
closedir($handle);
?>
</SELECT>
<INPUT type=submit value='Convert'>
</FORM>
<?
$XML = "";
if (is_uploaded_file($_FILES['XML']['tmp_name'])) {
$fp = fopen($_FILES['XML']['tmp_name'],"r") or DIE("Error
opening
XML");
$XML .= fread($fp,filesize($_FILES['XML']['tmp_name']));
fclose($fp);
} else {
if ($_POST['folderfile'] != "") {
$fp = fopen("../xml/".$_POST['folderfile'],"r") or DIE("Error
opening XML");
$XML .= fread($fp,filesize("../xml/".$_POST['folderfile']));
fclose($fp);
}
}
if ($XML != "") {
echo "Time to read: ".round(getmicrotime()-$time,2)."<BR>";
$time = getmicrotime();
if (preg_match("/^<\?xml/i",$XML)) {
$XML = preg_replace("/^<\?xml.+\?>/","",$XML);
} else {
die("Invalid XML format");
}
$tree = GetXMLTree($XML);
unset($XML);
echo "Time to array: ".round(getmicrotime()-$time,2)."<BR>";
$time = getmicrotime();
if (!$tree) {
die("Invalid XML (Err: Error parseing XML)");
}
$path = array_shift($tree);
array_shift($tree);
array_shift($tree);
array_shift($tree);
foreach($tree as $key => $val) {
if (!is_array($tree[$key])) {
DIE("Invalid XML (Err: Loop page)");
}
parse_page($tree[$key], $path);
}
unset($tree);
echo "Time to parse: ".round(getmicrotime()-$time,2)."<BR>";
}
?>
</BODY>
</HTML>
--EOF--
FUNC.PHP
--------
<?
function GetChildren($vals, &$i) {
$children = array();
if ($vals[$i]['value'])
array_push($children, $vals[$i]['value']);
while (++$i < count($vals)) {
switch ($vals[$i]['type']) {
case 'cdata':
array_push($children, $vals[$i]['value']);
break;
case 'complete':
$children[$vals[$i]['tag']] = $vals[$i]['value'];
break;
case 'open':
$children[] = GetChildren($vals,$i);
break;
case 'close':
return $children;
break;
}
}
}
function GetXMLTree($XML) {
echo "Creating parser... ";
$p = xml_parser_create();
echo "Setting enctype... ";
xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, "UTF-8");
echo "Parsing... ";
xml_parse_into_struct($p, stripslashes($XML), $vals, $index);
echo "Closing parser... ";
xml_parser_free($p);
unset($index);
$i = 0;
return GetChildren($vals, $i);
}
function getmicrotime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function parse_page($page, $folder) {
$file = $folder."\\".array_shift($page).".LDF<BR>";
echo "<PRE>";
print_r($page);
echo "</PRE>";
}
?>
--EOF--
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=17114&edit=1