0

I tryed to use answer from How to print pretty xml in javascript? but it didnt work for my code. It prints [object]. Is there some way to print xml in this object?

<!DOCTYPE html>
<html>
<head>
<script src="loadxmldoc.js"></script>
</head>
<body>

<script>

var xmldoc= loadXMLDoc('skoly.xml');

var d = document.createElement('div'); 
var t = document.createTextNode(xmldoc); 
d.appendChild(t);
document.write('<pre>' + d.innerHTML + '</pre>');


</script>
</body>
</html>

loadxmldoc.js:

function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // code for IE5 and IE6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
Community
  • 1
  • 1
Pinguanec
  • 19
  • 1

1 Answers1

0

The responseXML property will return an object of type Document if it is able to, otherwise null (see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#responseXML).

If all you're trying to do with the returned XML is to print it as text, you're probably best off just using the responseText property on the xhttp object instead, which will give you the XML as a string of text (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#responseText).

Your code for loadxmldoc.js should then become;

function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // code for IE5 and IE6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseText;
}

Also, as a side note, it's not really recommended to use synchronous XMLHttpRequests, since this blocks the main thread. As explained further on in that article (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#open()), this will be deprecated from FF 30

Pudge601
  • 2,048
  • 1
  • 12
  • 11