Reputation: 707
I'm trying to implement Google maps on my site. I'm following the tutorial here: https://developers.google.com/maps/articles/phpsqlajax_v3?hl=sv
However, I can't output xml properly when looping through the database. In the tutorial there are three examples on how to output xml. I get "error on line 2 at column 1: Extra content at the end of the document" both when using example two and three.
Som what's up with this, why am I getting this error message. Since the code is from Google I assume it's correct, so my best guess is that I have som kind of hidden sign in my file.
Any help much apprecieated!
<?php
require("dbinfo.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// 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");
// 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("name",$row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("type", $row['type']);
}
echo $dom->saveXML();
?>
Edit:
Here's an example of my output which might give some more insights on what's going on:
<marker name="Pan Africa Market" address="1521 1st Ave, Seattle, WA" lat="47.608940"
lng="-122.340141" type="restaurant">
<marker name="Buddha Thai & Bar" address="2222 2nd Ave, Seattle, WA"
lat="47.613590" lng="-122.344391" type="bar">
<marker name="The Melting Pot" address="14 Mercer St, Seattle, WA" lat="47.624561"
lng="-122.356445" type="restaurant">
</marker>
</marker>
</marker>
</marker>
So the problem seemts to be that the marker node doesn't close for each loop. Instead the closing nodes are just added at the end.
Upvotes: 0
Views: 808
Reputation: 6003
You have already assigned $node
to the markers node at the top and you are overwriting it in the while loop...that's why it's messing up after the first iteration. Replace your while
loop with this:
<?php
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$marker = $dom->createElement("marker");
$node->appendChild($marker);
$marker->setAttribute("name",$row['name']);
$marker->setAttribute("address", $row['address']);
$marker->setAttribute("lat", $row['lat']);
$marker->setAttribute("lng", $row['lng']);
$marker->setAttribute("type", $row['type']);
}
?>
Hope this helps.
Upvotes: 1