No XMLDocument from OpenLayers.Request.Post

2019-06-08 16:05发布

I use a asynchronous OpenLayers POST Request and get via responseText this String:

<?xml version="1.0" encoding="UTF-8"?>
<gml:TimePeriod xmlns:gml="http://www.opengis.net/gml">
 <gml:beginPosition>2011-10-18T15:15:00.000+02:00</gml:beginPosition>
<gml:endPosition>2014-11-23T14:45:00.000+01:00</gml:endPosition>
</gml:TimePeriod>

For some reason I do not get the response in a XML document object, respectively I got an empty XMLDocument.

My Code so far:

var request = OpenLayers.Request.POST({
                    url: "http://139.17.3.305:8080/database/sos",
                    async: true,    //is default
                    data: xmlString,
                    callback: handler   //name of triggered callback function
                });

//xml callback handler

function handler(request) {
                     var xmlText = request.responseText;    
                     console.log(xmlText); //returns the string above

                     var xmlDoc = request.responseXML;
                     console.log(xmlDoc);   // returns the empty XMLDocument    

                    var timeArray = xmlDoc.getElementsByTagName('TimePeriod');        
                    console.log("timeArray:",timeArray);

                  }; 

// create a XML Document

 function CreateXMLDocument () {
    var xmlDoc = request.responseText;
        if (window.DOMParser) {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString (xmlDoc, "text/xml");
        } else if (window.ActiveXObject) {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML (xmlDoc);
        }

        var TimeNode = xmlDoc.getElementsByTagName ("TimePeriod");
        var beginPosition = TimeNode.getAttribute ("beginPosition");
        alert ("The Timeperiod is " + beginPosition);
  }

Any idea how to get the “TimePeriod” tag into the objekt “timeArray”? Why does the request work for that string output and not for the XMLDocument?

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-08 16:59

I've figured it out! I received my requested time period in an alert by changing a few line of code. I exchanged the XML alert handler code and left the XML callback handler out.

//xml alert Handler

                  function handler(request) {
                    var xmlStr = request.responseText;  
                    console.log("xmlStr:",xmlStr);      

                    var parser=new DOMParser();
                    var xmlDoc=parser.parseFromString(request.responseText,"text/xml");
                    var gml = xmlDoc.getElementsByTagName("gml:beginPosition")[0].firstChild.data;  
                    console.log("gml:beginPosition", gml);
                    alert(gml);
                  }; 
查看更多
登录 后发表回答