Loneferret Posted April 26, 2008 Share Posted April 26, 2008 Hey there, got this problem with xml_parse() all of a sudden. Got a small script running, and all was fine until a month ago (perhaps 2). Keep getting : XML error: XML declaration not finished at line 1 The resource is an Amazon feed, so I know the xml generated is good. Script itself has not been touched since conception. The only thing that has changed is a server upgrade about 2 months ago I guess. Running PHP Version 5.2.5 on Fedora 7 Hosted at godaddy .. euh... what else can I say? Any help would be greatly appreciated... Link to comment https://forums.phpfreaks.com/topic/103020-xml_parse-sudden-error/ Share on other sites More sharing options...
Loneferret Posted April 26, 2008 Author Share Posted April 26, 2008 Ok.. hehe here's some more info.. If you want to see what I mean, here's a link to the page that DID work a few months ago.. http://www.bindex.com/xmlsearch.php This is the xmlsearch.php which is basically where one enters a query ----------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: small; font-weight: bold; } --> </style> </head> <body> <form name="formkeyword" method="post" action="xmlparser.php"> <table width="400" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="150"><span class="style1">Keyword : </span></td> <td width="250"><input type="text" name="keyword" style="font-family: Verdana; font-size: 8pt"></td> </tr> <tr> <td width="150" class="style1">Item : </td> <td width="250"> <select name="item" style="font-family: Verdana; font-size: 8pt"> <option value="Music">Music</option> <option value="DVD">DVD</option> </select> </td> </tr> <tr> <td colspan="2"><div align="center"><input type="submit" name="submit" value="Submit" style="font-family: Verdana; font-size: 8pt"></div></td> </tr> </table> </form> <?php include('includes/db_connect.php'); $sel_from_dvd = "SELECT * FROM dvd"; $get_inf_dvd = mysql_query($sel_from_dvd) or die(mysql_error()."<br>ERROR : get_inf_dvd"); $nb_dvd = mysql_num_rows($get_inf_dvd); $sel_from_music = "SELECT * FROM music"; $get_inf_music = mysql_query($sel_from_music) or die(mysql_error()."<br>ERROR : get_inf_music"); $nb_music = mysql_num_rows($get_inf_music); ?> <table width="250" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="150" class="style1">DVD : </td> <td width="100" class="style1"><div align="center"><?php print $nb_dvd; ?></div></td> </tr> <tr> <td width="150" class="style1">MUSIC : </td> <td width="100" class="style1"><div align="center"><?php print $nb_music; ?></div></td> </tr> </table> </body> </html> -------------- Here's where the error comes up. It's a simple example that was taken from php.net file is called xmlparser.php <?php if(!$keyword) { header("location: xmlsearch.php"); } ?> <head> <title></title> </head> <body> <h2>Test Search <?php print $item." ".$keyword; ?> <a href="xmlsearch.php">Home</a></h2> <dl> <?php include('includes/db_connect.php'); //VARIABLES FOR INFO $insideitem = false; $tag = ""; $title = ""; $link = ""; $ASINs = ""; $prodgroup = ""; $totres = ""; $totpage = ""; if($page) { $page = (int)$page; } function startElement($parser, $name, $attrs) { global $insideitem, $tag, $ASINs, $title, $prodgroup, $link, $totres, $totpage, $urls, $width, $height, $keyword, $item; if ($insideitem) { $tag = $name; } elseif ($name == "ITEMS") { $insideitem = true; } //print $name."<br>"; } function endElement($parser, $name) { global $insideitem, $tag, $ASINs, $title, $prodgroup, $link, $totres, $totpage, $urls, $width, $height, $x, $keyword, $item, $page; if ($name == "ITEMS") { print "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n"; print "<tr>\n"; print "<td width=\"750\">\n"; for ($x=1;$x<=$totpage;$x++) { if($x == $totpage) { print "<font color=\"#000000\" face=\"Verdana\" size=\"2\"><a href=\"xmlparser.php?keyword=".$keyword."&page=".$x."&item=".$item."\">".$x."</a></font>\n"; } elseif($x == 1) { } else { print "<font color=\"#000000\" face=\"Verdana\" size=\"2\"><a href=\"xmlparser.php?keyword=".$keyword."&page=".$x."&item=".$item."\">".$x."</a> - </font>\n"; } } print "</td>\n"; print "</tr>\n"; print "</table>\n"; $totres = ""; $totpage = ""; $insideitem = false; } if($name == "TOTALRESULTS") { $totres = htmlspecialchars(trim($totres)); print "Number of Result : ".$totres."<br><br>"; } if($name == "TOTALPAGES") { $totpage = htmlspecialchars(trim($totpage)); print "Number of Pages : ".$totpage."<br><br>"; print "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n"; print "<tr>\n"; print "<td width=\"750\">\n"; for ($x=1;$x<=$totpage;$x++) { if($x == $totpage) { print "<font color=\"#000000\" face=\"Verdana\" size=\"2\"><a href=\"xmlparser.php?keyword=".$keyword."&page=".$x."&item=".$item."\">".$x."</a></font>\n"; } else { print "<font color=\"#000000\" face=\"Verdana\" size=\"2\"><a href=\"xmlparser.php?keyword=".$keyword."&page=".$x."&item=".$item."\">".$x."</a> - </font>\n"; } } print "</td>\n"; print "</tr>\n"; print "</table>\n"; } if($name == "ITEM") { if(!$page) { $page=1; } $ASINs = htmlspecialchars(trim($ASINs)); $title = htmlspecialchars(trim($title)); $prodgroup = htmlspecialchars(trim($prodgroup)); print "<table width=\"500\" cellspacing=\"0\" cellpadding=\"0\" border=\"1\" bordercolor=\"#CCCCCC\" align=\"center\">\n"; print "<tr>\n"; print "<td width=\"75\">"; if($prodgroup == "DVD") { print "<a href=\"xmlresultdvd.php?ASIN=".$ASINs."&title=".$title."&keyword=".$keyword."&page=".$page."&item=DVD\"><img src=\"http://images.amazon.com/images/P/".$ASINs.".01._SCTHUMBZZZ_.jpg\" border=\"0\"></a></td>\n"; } elseif($prodgroup == "Music") { print "<a href=\"xmlresultmusic.php?ASIN=".$ASINs."&title=".$title."&keyword=".$keyword."&page=".$page."&item=Music\"><img src=\"http://images.amazon.com/images/P/".$ASINs.".01._SCTHUMBZZZ_.jpg\" border=\"0\"></a></td>\n"; } print "<td width=\"325\" align=\"left\" valign=\"middle\"><font color=\"#000000\" face=\"Verdana\" size=\"2\"><strong>TITLE: </strong>"; if($prodgroup == "DVD") { print "<a href=\"xmlresultdvd.php?ASIN=".$ASINs."&title=".$title."&keyword=".$keyword."&page=".$page."&item=DVD\">".$title."</a></font></td>\n"; } elseif($prodgroup == "Music") { print "<a href=\"xmlresultmusic.php?ASIN=".$ASINs."&title=".$title."&keyword=".$keyword."&page=".$page."&item=Music\">".$title."</a></font></td>\n"; } print "<td width=\"100\" align=\"center\" valign=\"middle\"><font color=\"#000000\" face=\"Verdana\" size=\"2\"><strong>".$prodgroup."</strong></font></td>\n"; print "</tr>\n"; print "<tr>\n"; if($prodgroup == "DVD") { $sel_inf_dvd = "SELECT * FROM dvd WHERE ASIN='$ASINs'"; $get_inf_dvd = mysql_query($sel_inf_dvd) or die(mysql_error()."<br>ERROR : get_inf_dvd"); $chkrow = mysql_num_rows($get_inf_dvd); } elseif($prodgroup == "Music") { $sel_inf_cd = "SELECT * FROM music WHERE ASIN='$ASINs'"; $get_inf_cd = mysql_query($sel_inf_cd) or die(mysql_error()."<br>ERROR : get_inf_cd"); $chkrow = mysql_num_rows($get_inf_cd); } if($chkrow >= 1) { print "<td colspan=\"3\" valign=\"top\"><font color=\"#000000\" face=\"Verdana\" size=\"2\"><strong>ASIN: </strong>".$ASINs." IN</font></td>\n"; } else { print "<td colspan=\"3\" valign=\"top\"><font color=\"#000000\" face=\"Verdana\" size=\"2\"><strong>ASIN: </strong>".$ASINs." OUT</font></td>\n"; } print "</tr>\n"; print "</table>\n"; print "<br>"; $ASINs = ""; $title = ""; $prodgroup = ""; } } function characterData($parser, $data) { global $insideitem, $tag, $ASINs, $title, $prodgroup, $link, $totres, $totpage, $urls, $width, $height, $keyword, $item; if ($insideitem) { switch ($tag) { case "TOTALRESULTS": $totres .= $data; break; case "TOTALPAGES": $totpage .= $data; break; case "ASIN": $ASINs .= $data; break; case "TITLE": $title .= $data; break; case "PRODUCTGROUP": $prodgroup .= $data; break; case "DETAILPAGEURL": $link .= $data; break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if($keyword) { $str1 = " "; $rep1 = "%20"; $keyword = str_replace($str1, $rep1, $keyword); } //THATS EXEMPLE FOR TRACKS AND DVD INFO //http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=0SCYH95EC1EW16DYYK02&Operation=ItemLookup&ItemId=B000CCBC9E&ResponseGroup=Medium //THATS EXEMPLE FOR IMAGES //http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=0SCYH95EC1EW16DYYK02&Operation=ItemLookup&ItemId=B00006GFAB&ResponseGroup=Images if(!$page) { $fp = fopen("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=0SCYH95EC1EW16DYYK02&Operation=ItemSearch&Keywords=".$keyword."&SearchIndex=".$item."&ItemPage=1","r") or die("Error reading RSS data."); } else { $fp = fopen("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=0SCYH95EC1EW16DYYK02&Operation=ItemSearch&Keywords=".$keyword."&SearchIndex=".$item."&ItemPage=".$page."","r") or die("Error reading RSS 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); ?> </dl> </body> </html> --------- Hope this helps anyone find my problem faster. Link to comment https://forums.phpfreaks.com/topic/103020-xml_parse-sudden-error/#findComment-527718 Share on other sites More sharing options...
Loneferret Posted April 26, 2008 Author Share Posted April 26, 2008 bump... sorry Link to comment https://forums.phpfreaks.com/topic/103020-xml_parse-sudden-error/#findComment-528001 Share on other sites More sharing options...
hitman6003 Posted April 26, 2008 Share Posted April 26, 2008 Since you are using php5, I would change to using the simplexml functions. http://www.php.net/book.simplexml Link to comment https://forums.phpfreaks.com/topic/103020-xml_parse-sudden-error/#findComment-528020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.