Using window.open, document.open, and document.write to display XML (XML rendering gone)

This is related to another question, but is <strong>not</strong> a duplicate. It deals with a proposed solution that I have reached an impasse.

I have the following code that reads an XML, makes changes, opens a window, and writes the XML into the document. The problem is that the content is not rendered as XML. Any way to set a content type, etc, to have the browser handle the content as XML?

<script> var wxml; var xDoc; var xDevices, xInputs; var xDevice, xInput; function fSetXmlAInput(iDevice, iNode, nodeNewVal) { xInput = xInputs[iNode]; xValue = xInput.getElementsByTagName("value")[0]; // change node value: // console.log("nodeVal: " + xValue.firstChild.nodeValue); xValue.firstChild.nodeValue = nodeNewVal; // console.log("newVal: " + xValue.firstChild.nodeValue); } function fSetXmlDevice(iDevice) { xDevice = xDevices[iDevice]; xInputs = xDevice.getElementsByTagName("input"); fSetXmlAInput(iDevice, 0, "22"); fSetXmlAInput(iDevice, 1, "33"); } function alternativeLoadXML3() { // load xml file if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); } else { // IE 5/6 xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET", "my_template.xml", false); xhttp.send(); xDoc = xhttp.responseXML; xDevices = xDoc.getElementsByTagName("device"); fSetXmlDevice(1); var xmlText = serializeXmlNode(xDoc); var newWindow = window.open("my_template.xml", "Test", "width=300,height=300,scrollbars=1,resizable=1"); newWindow.document.open(); newWindow.document.write(xmlText); newWindow.document.close() }; </script>

Below the XML as well:

<?xml version="1.0" encoding="utf-8"?> <myXmlRoot> <device> <input><name>"name 1"</name><value>{replaceMe!}</value></input> <input><name>"name 2"</name><value>{replaceMe!}</value></input> </device> <device> <input><name>"name 1"</name><value>{replaceMe!}</value></input> <input><name>"name 2"</name><value>{replaceMe!}</value></input> </device> <device> <input><name>"name 1"</name><value>{replaceMe!}</value></input> <input><name>"name 2"</name><value>{replaceMe!}</value></input> </device> </myXmlRoot>

Any way to force the browser to render content in new window as XML...or does using document.open and document.write mean code is rendered as HTML?

Related: Change XML content using JavaScript, missing refresh


Both document.open and document.write are used to write HTML or Javascript to a document. This goes back to the DOM Level 2 Specification of document.open, which assumes that the document is opened in order to write unparsed HTML.

Instead of using document.open and document.write, I would instead suggest dymanically adding elements using the XML DOM as in sampleElement.appendChild(XMLnode)

A similar question can also be found here: how to display xml in javascript?


Use dataURI to write xml into new window is very easy.

     "Test", "width=300,height=300,scrollbars=1,resizable=1");