计算 XML 文档元素会产生类型问题:null 不是对象



使用sql/google地图教程中的phpsqlajax.php脚本,似乎我已经设法用mysql的值生成xml元素(标记)。尽管没有内容被回显到页面,但标记列表正在根据控制台日志通过 mysql 填充。
1. 这是否意味着 xml 正在正确生成?

我问是因为phpsqlajax_map_v3.php脚本收到一个错误报告:计算 XML 文档元素给出类型问题 null 不是对象。
2. 这似乎是在说标记节点是空的,因此没有从 mysql 获取值。

代码:
\\\已编辑的工作脚本\\\
phpsqlajax.php Google 教程链接

<?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.由于这是从脚本生成xml,我将如何将浏览器指向xml以查看它是否有效或有效?

我已经在stackoverflow上搜索了类似的问题,但是似乎这些修复程序在这里不适用,即内容类型:xml,谷歌代码教程中的各种语法错误,代码结构等。

删除echo 'sueccess';echo 'success';

XML文档必须以<?xml开头,当您使用这些行时,它将以sueccesssuccess<?xml开头,解析失败。

当您不想收到有效的 XML 文档时,不要在页面内回显任何内容($dom->saveXML() 除外)。

最新更新