gpt4 book ai didi

javascript - 使用 window.open、document.open 和 document.write 显示 XML(XML 渲染消失)

转载 作者:行者123 更新时间:2023-11-29 18:23:02 25 4
gpt4 key购买 nike

这与另一个问题有关,但重复。它涉及我已陷入僵局的提议解决方案。

我有以下读取 XML、进行更改、打开窗口并将 XML 写入文档的代码。问题是内容没有呈现为 XML。有什么方法可以设置内容类型等,让浏览器将内容作为 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...或者使用 document.open 和 document.write 是否意味着代码呈现为 HTML?

相关:Change XML content using JavaScript, missing refresh

最佳答案

使用 dataURI 将 xml 写入新窗口非常容易。

window.open('data:text/xml,'+encodeURIComponent(xmlText),
"Test", "width=300,height=300,scrollbars=1,resizable=1");

关于javascript - 使用 window.open、document.open 和 document.write 显示 XML(XML 渲染消失),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com