XML error from code that is correct according to Google

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!

Moderator: General Moderators

Post Reply
Simbachips
Forum Newbie
Posts: 11
Joined: Tue Apr 19, 2011 8:20 am

XML error from code that is correct according to Google

Post by Simbachips »

I have tried two methods that is shown on this site http://code.google.com/apis/maps/articl ... ax_v3.html

This one is using DOM

Code: Select all

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server
$chandle = mysql_connect("localhost", $username, $password) 
    or die("Connection Failure to Database".mysql_error());
echo "Connected to database server<br>";

mysql_select_db($dbName, $chandle) or die ($dbname . " Database not found." . $username);
echo "Database " .  $dbName . " is selected";

$query = "SELECT * FROM test_1 where 1";
$result = mysql_query($query)
	or die('Invalid query: ' .mysql_error());
echo "<br>Table successfuly retrieved<br>";
 
header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("street",$row['street']);
  $newnode->setAttribute("suburb", $row['suburb']);  
} 
echo $dom->saveXML();
?>
and this echo

Code: Select all

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','<',$htmlStr); 
$xmlStr=str_replace('>','>',$xmlStr); 
$xmlStr=str_replace('"','"',$xmlStr); 
$xmlStr=str_replace("'",''',$xmlStr); 
$xmlStr=str_replace("&",'&',$xmlStr); 
return $xmlStr; 
} 
$chandle = mysql_connect($dbHost, $username, $password) 
    or die("Connection Failure to Database".mysql_error());
echo "Connected to database server<br>";

mysql_select_db($dbName, $chandle) or die ($dbname . " Database not found." . $username);
echo "Database " .  $dbName . " is selected";

$query = "SELECT * FROM test_1";
$result = mysql_query($query)
	or die('Invalid query: ' .mysql_error());
echo "<br>Table successfuly retrieved";

header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'street="' . parseToXML(utf8_encode($row['street'])) . '" ';
  echo 'suburb="' . parseToXML(utf8_encode($row['suburb'])) . '" ';
  echo '/>';
}
// End XML file
echo '</markers>';

mysql_close($chandle);
The sql queries do work as I can print out values retrieved from the DB

The error I get in both instances is:

This page contains the following errors:

error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.

Nothing is rendered past this point...
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: XML error from code that is correct according to Google

Post by Jade »

Remove the @ sign your fetch results: while ($row = @mysql_fetch_assoc($result)){

I bet you're not getting any query results.
Post Reply