-2

我试图检索 XML 并将其放入 var 标记中,当我运行它时

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

我收到一条错误消息,说 XML 为空,这是我在运行生成 XML 的文件时得到的 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>

这是一些周围的代码

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);
}
4

1 回答 1

1

我已经使用我生成的 XML运行了上一个问题中的代码,没有任何错误。如果header("Content-type: text/xml")在我的文件中删除了该行,我会收到错误Uncaught TypeError: Cannot read property 'documentElement' of null这表明您没有发送标头。请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 发送。

尝试

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
于 2013-02-12T15:16:16.090 回答