Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

php - google maps js v3 xml is null

Im trying to retrieve the XML and put it into the var markers, when I run it the line

var markers = xml.documentElement.getElementsByTagName("marker");

I get an error saying XML is null, here is the XML I get when running the file that produces the XML

This XML file does not appear to have any style information associated with it. The document tree is shown below.
  <markers>
 <marker name="funtimes" address="5451 Forest Cove Lane" lat="34.1519" lng="-118.774717" type="Basketball" desc="going to play fun times with fun people" date="2012-12-29" time="23:00:00" eventcap="4" eventcur="10" eventid="1" admin="true"/>
 <marker name="Event test" address="1231 fake st" lat="34.1517" lng="-118.77234" type="Cycling" desc="just a description" date="2013-01-31" time="12:00:00" eventcap="10" eventcur="4" eventid="2" admin="true"/>
 <marker name="Event test 2" address="1231 fake st" lat="34.1512" lng="-118.77421" type="Football" desc="just a description 2" date="2013-01-31" time="12:00:00" eventcap="10" eventcur="3" eventid="3" admin="true"/>
 <marker name="funtimes" address="5451 Forest Cove Lane" lat="34.1519" lng="-118.774717" type="Basketball" desc="going to play fun times with fun people" date="2012-12-29" time="23:00:00" eventcap="4" eventcur="10" eventid="1" admin="false"/>    
 <marker name="Event test" address="1231 fake st" lat="34.1517" lng="-118.77234" type="Cycling" desc="just a description" date="2013-01-31" time="12:00:00" eventcap="10" eventcur="4" eventid="2" admin="false"/>
 <marker name="Event test 2" address="1231 fake st" lat="34.1512" lng="-118.77421" type="Football" desc="just a description 2" date="2013-01-31" time="12:00:00" eventcap="10" eventcur="3" eventid="3" admin="false"/>
 <marker name="Event test 3" address="2142 fake st" lat="34.1602" lng="-118.77789" type="Football" desc="just a description 3" date="2013-01-28" time="13:00:00" eventcap="8" eventcur="2" eventid="4" admin="false"/>
 </markers>

Here is some of the surrounding code

downloadUrl("mapXML2.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have run your code from previous question with my generated XML with no errors. If the line header("Content-type: text/xml") is removed in my file I get the error Uncaught TypeError: Cannot read property 'documentElement' of null This shows you are not sending the header. Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Try

header("Content-type: text/xml");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
//Connect to database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...