note 35051 added to function.xml-parse
| From: | jef at eyeintegrated dot com | Date: | Mon, 18 Aug 2003 03:08:39 +0000 |
| Subject: | note 35051 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54550@lists.php.net to get a copy of this message | ||
xml_parse() seems kinda dumb to me as to the
third formal parameter helping in parsing
a stream. The below snippet is useful for
parsing a stream, instead of having to
read in the entire stream before parsing
to avoid potential in-data/in-tag cut offs.
This is used to read in a 300 meg+ plus
of <JOB>'s, about 100,000 of them.
frag = ""; // leftover for stream
while(!feof($fp)) {
$stream = $frag . fread($fp,FILE_READ_BLOCK);
$RECS = preg_split("/<\/JOB>/",$stream);
$delims = count($RECS) - 1;
$frag = $RECS[$delims];
#== if no split, no </JOB> found ==
if($delims <= 0) { continue; }
#== RECORDS LOOP ==
for($i=0; $i < $delims; $i++) {
$block = $RECS[$i] . "</JOB>";
#== heh, high overhead, good for errors ==
$A_data = explode("\n",$stream);
$c_lines = count($A_data);
$LastLines = $FileLInes;
$FileLines += $c_lines;
#== parse this block ==
$parseOK = xml_parse($XML_PARSER,$block,false);
if(!$parseOK && !feof($fp)) {
$Eline = xml_get_current_line_number($XML_PARSER);
$Ecode = xml_get_error_code($XML_PARSER);
$Estr = xml_error_string($Ecode);
$msg = "XML ERROR\n";
$msg .= "XML error code: $Ecode\n";
$msg .= "XML error string: $Estr\n";
$msg .= "XML error on input line: $Eline\n";
$msg .= "Current START tag: <$C_START_TAG>\n";
$msg .= "Current END tag: </$C_END_TAG>\n";
$msg .= "Last tag processed: $C_LAST_TAG\n";
$msg .= "Records already loaded: $TotRecs\n";
$BadLineNum = $Eline - $LastLines -1;
$BadLine = $A_data[$BadLineNum];
fatal($msg,__LINE__,$BadLine,"Suspected bad input line");
}
} // END RECORDS LOOP
}
xml_parser_free($XML_PARSER);
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35051
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35051&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35051&report=yes