2

This is related to another question, but is not 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

2 Answers 2

3

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

window.open('data:text/xml,'+encodeURIComponent(xmlText),
     "Test", "width=300,height=300,scrollbars=1,resizable=1");
Sign up to request clarification or add additional context in comments.

1 Comment

You legend. Works great in Chrome (not tested in other browsers)
1

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?

2 Comments

So how would you use sampleElement.appendChild(XMLnode) in the above example?
You would need to load my_template.xml using an XML Parser like you have used in function alternativeLoadXML3(). Then, you can use the XML DOM to append elements to the document. Finally, to display the XML as a collapsable tree, you can use a JavaScript framework such as those suggested at: stackoverflow.com/questions/1366179/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.