PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello all. I have the following script that is meant to fetch the filenames of 5 xml files from a database and then parse them. The script will parse the first xml file fine but then returns the following error:
$result = mysql_query("SELECT FILENAME FROM blog ORDER BY FILENAME DESC LIMIT 5");
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
$filename = "admin/$row[FILENAME]";
if (!$fp = fopen($filename, "r")) {die ("Cannot open the file");}
while ($data = fread($fp, 4096))
{
if (!xml_parse($xmlparser, $data, feof($fp)))
{
die (sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlparser)), xml_get_current_line_number($xmlparser)));
}
}
$currenttag = null;
$name = null;
$data = null;
}
Both what i've got and what you recommend fetch the filename since, as i mentioned, the first xml file is parsed just fine. It's when it tries to parse a second xml file that the error occurs.
If this is the case, well.. then the time is for a little basic debugging maybe... do a print_r() of the mysql_fetches and see if there is a problem...
Each xml file is successfully fetched and assigined to $filename. It's also successfully opened and read using fread(). The problem occurs at the end with xml_parse($xmlparser, $data, feof($fp)).
timvm, I suspect the problem might be along the line of what you're saying but it doesn't actually make since for it to be so since i'm not joining the xml files into one $data variable. Nonetheless that's why i included:
In view of above comments and data being parsed correctly, the only one thing remains, and that is what timvw pointed to. each xml doc has one root element, beyond that xml is not valid.
So, i would suggest one thing now, it looks like you are creating xmlparser somewhere outside the loop. Move that to the start of loop and add a xml_parser_free() to the end of your loop. That should solve the problem hopefully.
I seriously advise you to destroy the xml parser object every time once you read a XML file and recreate another to read a new XML file.
I had a similar problem but mine was implemented in OO. viewtopic.php?t=37127&highlight= http://raghavan20.allhyper.com/xmlParser.php (original file with the same error)
In my case, I had to unset the class object and recreate to read another file which I dont wanted to do but that was the only way it worked.
if (!$fp = fopen($filename, "r")) {die ("Cannot open the file");}
while ($data = fread($fp, 4096))
{
if (!xml_parse($xmlparser, $data, feof($fp)))
{
die (sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlparser)), xml_get_current_line_number($xmlparser)));
}
}
if (!$fp = fopen($filename, "r")) {die ("Cannot open the file");}
else{//look at the else here; you should read only if the $fp is not null
while ($data = fread($fp, 4096))
{
if (!xml_parse($xmlparser, $data, feof($fp)))
{
die (sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlparser)), xml_get_current_line_number($xmlparser)));
}
}
}