Krazy Bakon
Krazy Bakon

Reputation: 28

Im getting the error "TypeError: xml.getElementsByTagName is not a function"

I am getting the error "TypeError: xml.getElementsByTagName is not a function"

the error lies in "var xmlDoc = new DOMParser().parseFromString(xml,'text/xml');"

how can i fix this ? i have been working on this for hours on end and still no result

<!DOCTYPE html>
<html>
 <meta charset="UTF-8"> 
<body>

<p id="demo"></p>

<script>

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {

   if (xhttp.readyState == 4 && xhttp.status == 200) {
       myFunction(xhttp);
    }
}
xhttp.open("GET", "http://LEMONPIE-PC/erdas-iws/ogc/wms/?service=WMS&request=getcapabilities", true);
xhttp.send();

function myFunction(xml) {

    var xmlDoc = new DOMParser().parseFromString(xml,'text/xml');

    console.log(xmlDoc);

    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("Layer");
    for (i=0;i<x.length;i++)
    { 
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("Layer")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("Style")[0].childNodes[0].nodeValue);
        document.write("</td></tr>");
    }
    document.write("</table>");


}



</script>

</body>
</html>

Upvotes: 0

Views: 1977

Answers (1)

epascarello
epascarello

Reputation: 207501

myFunction(xhttp);  <-- Look at what you are passing to the method

You are passing in the XMLHttpRequest object and treating that as the text, when that is not the case. You need to reference the responseText that XMLHttpRequest object holds.

myFunction(xhttp.responseText);

or if you are fetching XML, I not sure why you are parsing it again since the XMLHttpRequest object will do it for you. It should be available with responseXML as long as it is a valid XML document.

myFunction(xhttp.responseXML);  

Upvotes: 2

Related Questions