0

Using the phpsqlajax.php script from the sql/google maps tutorial, it seems that I have managed to generate xml elements(markers) with values from mysql. Though nothing is being echoed to the page, the markers list is being populated via mysql according to the console log.
1. Does that necessarily mean the xml is being generated properly?

I ask because the phpsqlajax_map_v3.php script gets one error report: evaluating xml document element gives type issue null is not an object.
2. That seems to be saying that the marker node is empty, and therefore not getting the values from mysql.

the code:
\\\EDITED WORKING SCRIPT\\\
phpsqlajax.php google tutorial link

<?php

// Start XML file, create parent node

    $dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 
?>
<?  
    // connect
    $host = "localhost";
    $username = 'root';
    $psswrd = 'root';
    $db = 'sql_maps';
    $dbc = mysqli_connect($host, $username, $psswrd, $db);

    if(!mysqli_connect_errno() ) {
        } else { 
        die("Database failed: " . 
        mysqli_connect_error() .
        " ( " . mysqli_connect_errno() . " )"
        );

    }
?>
<?php
// Using PHP's domxml Functions to Output XML
// Select all the rows in the markers table

    // get data
    $query = "SELECT * FROM markers WHERE 1";

    // catch resource(collection of database rows)
    $result = mysqli_query($dbc, $query);
    // check
    if($result) {

    } else {
        die("connection failed");
    }



header("Content-type:  application/xml"); 

while ($row = mysqli_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();

?>

phpsqlajax_map_v3.php

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps AJAX + mySQL/PHP Example</title>

            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=keyIntentionallyLeftOut&sensor=false">
    </script>
    <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      restaurant: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };
    var map;
    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("../geo_scripts/phpsqlajax_genxml2.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 address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address;
          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 bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    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() {}

    //]]>

  </script>
  </head>

  <body onload="load()">
    <div id="map" style="width: 500px; height: 300px"></div>
  </body>
</html>

3. Since this is generating xml from a script, how would I point the browser to the xml to see if it is valid or working?

I've searched similar questions on stackoverflow, however it seems those fixes do not apply here, i.e.- content-type: xml, various syntax errors from google code tutorial, code structure, and more.

4

1 回答 1

0

Remove the echo 'sueccess'; and echo 'success'; .

A XML-document has to start with <?xml, when you use these lines it will start with sueccesssuccess<?xml and the parsing fails.

Don't echo anything inside the page (except $dom->saveXML()) when you wan't to receive a valid XML-document.

于 2013-10-26T20:30:01.377 回答