Christina
Christina

Reputation: 19

Error parsing attribute when generating XML from PHP

I was trying to implement google map in my website by using the data in MySQL but ended up getting this error

This page contains the following errors:

error on line 1 at column 18: error parsing attribute name

Below is a rendering of the page up to the first error.

Below is the code provided by: https://developers.google.com/maps/documentation/javascript/mysql-to-maps

May I know what would be causing the error?

<?php
require("connect.php");

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}

// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

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 'id="' . $ind . '" ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}

// End XML file
echo '</markers>';

?>

Upvotes: 1

Views: 1174

Answers (1)

Fraser
Fraser

Reputation: 17039

It looks like the example is wrong.

echo 'id="' . $ind . '" ';

Should be

echo 'id="' . $row['id'] . '" ';

(Failing that try removing the error suppression from the mysql_fetch_assoc call to see if the database is throwing an error. i.e. remove @)

In either case what is happening is this.

You are outputting a XML document, the document is being rendered - but you get

error on line 1 at column 18: error parsing attribute name

because the XML is malformed at the name attribute- this would tell me that you almost certainly have a php error in the XML output.

e.g.

<markers>
    <marker id="Notice: Undefined variable: "id" in ~\whatever\kml.php on line blah blah blah
" name="foo" address="bar" ... />}

You can confirm this by calling the file via curl or similar, or by "viewing source" in most browsers.

Finally if that isn't the case then try turning on error reporting at the top of your file right after the opening php tag, eg.

<?php
ini_set('display_errors', 1);
error_reporting(~0);

Then checking the actual output of the script again.

Upvotes: 1

Related Questions