本文介绍了使用window.open,document.open和document.write来显示XML(XML渲染消失)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与另一个问题有关,但是不是重复。
它处理了一个我已经陷入僵局的提议解决方案。

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

我有以下代码读取XML,进行更改,打开窗口,以及将XML写入文档。问题是内容没有呈现为XML。
任何设置内容类型等的方法,让浏览器将内容处理为XML?

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>

以下XML:

<?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>

任何方式强制浏览器在新窗口中将内容呈现为XML ...或使用文档.open和document.write意味着代码呈现为HTML?

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?

相关:

推荐答案

document.write 用于将HTML或Javascript写入文档。这可以追溯到,它假定打开文档以写入未解析的HTML。

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.

而不是使用 document.open document.write ,而是建议使用XML DOM在dymanically添加元素,如 sampleElement.appendChild(XMLnode)

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?

这篇关于使用window.open,document.open和document.write来显示XML(XML渲染消失)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:56