note 35051 rejected from function.xml-parse by didou

From: Date: Thu, 03 Feb 2005 19:49:15 +0000
Subject: note 35051 rejected from function.xml-parse by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-84468@lists.php.net to get a copy of this message
Note Submitter: jef at eyeintegrated dot com ---- 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);

« previous php.notes (#84468) next »