[thelist] placing the pointer in a text file using PHP..

Carl J Meyer cjmeyer at npcc.net
Mon Dec 30 16:35:01 CST 2002


Tom,

You've got the idea.  I haven't done lots of XML parsing in PHP, but it
looks to me like the easiest route would be to use a flag variable for
parsing the xml and only set it to true once you've found <?xml:

(this all replaces your single while statement)
-----------------
$parsing = false;
while ($data = fread($fp, 4096))
	{
	if(preg_match('/<\?xml/', $data))
		{
		$data = preg_replace('/^.*<\?xml/', '<?xml', $data);
		$parsing = true;
		}
	if(!$parsing) continue;
	xml_parse($xml_parser, $data, feof($fp))
		or die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($xml_parser)),
			xml_get_current_line_number($xml_parser)));
	}
------------------

No guarantees this is the best or cleanest way (just the quickest thing
off the top of my head), but it should work.  Of course it will break if
the string <?xml should randomly appear in the midst of your message
headers, but that shouldn't ever happen.

Carl

On Mon, 2002-12-30 at 14:15, Tom Dell'Aringa wrote:
> -------------
> $xml_parser = xml_parser_create();
> xml_set_element_handler($xml_parser, "startElement", "endElement");
> xml_set_character_data_handler($xml_parser, "characterData");
> $fp = fopen("http://www.silverheaven.com/reg/dump.xml", "r")
> 	or die("Error reading data.");
> while ($data = fread($fp, 4096))
> 	xml_parse($xml_parser, $data, feof($fp))
> 		or die(sprintf("XML error: %s at line %d",
> 			xml_error_string(xml_get_error_code($xml_parser)),
> 			xml_get_current_line_number($xml_parser)));
> fclose($fp);
> xml_parser_free($xml_parser);
> ----------
>
> so $fp = fopen("http://www.silverheaven.com/reg/dump.xml", "r") is
> where I pull the file in - so you are saying I would strip the info
> here...that makes sense. What would be the best method to do that?
> That would be great if I could pull it at this point, then when it
> hits the while loop, the file will be valid xml and I will have no
> problem.





More information about the thelist mailing list