几天来,我一直在试图找出我的代码出了什么问题,但我似乎无法破解它。我正在努力跟随this tutorial
我尝试了所有建议的三种方法从sql server输出XML,但我只能使用echo使其工作。
我遇到的问题是我一直在犯错误
“TypeError:xml为空”
这发生在var markers = xml.documentElement.getElementsByTagName("marker");
在下面发布的代码中。
我不知道如何继续。如果有任何问题我可以问你们,请告诉我。Here 是代码尝试运行的网页
代码如下:
(标题部分(编辑为相关代码))
<?php
<!-- Google Maps Scrips -->
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyA-ZWuoso96viUofF_enb79SiN7QwYIh1o&sensor=false">
</script>
<script type="text/javascript">
//Variables used in maps
var geocoder;
var map;
var markersArray = [];
var infos = [];
geocoder = new google.maps.Geocoder();
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(47.6, -122.3),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("content_map"),
mapOptions);
var infoWindow = new google.maps.InfoWindow;
google.maps.event.addListener(map, \'click\', function(event) {
placeMarker(event.latLng, map);
});
// loads the xml data from marker database
downloadUrl("http://wp-content/themes/oxygen-child/phpsqlajax_genxml3.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var type = markers[i].getAttribute("type");
var description = markers[i].getAttribute("description");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var rank = markers[i].getAttribute("rank");
var html = "<b>" + name + "</b> <br/>" + description;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject(\'Microsoft.XMLHTTP\') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open(\'GET\', url, true);
request.send(null);
}
function doNothing() {}
//]]>
function placeMarker(location, map) {
var marker = new google.maps.Marker({
position: location,
map: map
});
map.panTo(position);
}
</script>`
(phpsqlajax\\u genxml3.php)
<?php
require("phpsqlajax_dbinfo.php");
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;
}
// 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 \'name="\' . parseToXML($row[\'name\']) . \'" \';
echo \'type="\' . $row[\'type\'] . \'" \';
echo \'description="\' . parseToXML($row[\'description\']) . \'" \';
echo \'lat="\' . $row[\'lat\'] . \'" \';
echo \'lng="\' . $row[\'lng\'] . \'" \';
echo \'rank="\' . $row[\'rank\'] . \'" \';
echo \'/>\';
}
// End XML file
echo \'</markers>\';
?>
`