使用 JavaScript 在另一个窗口中打开 XML 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15357295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open XML content in another window, using JavaScript
提问by Atara
I understand I cannot save XML content to a local file, because of security restrictions. but is there a way I can show the XML content in another browser window, as
我了解由于安全限制,我无法将 XML 内容保存到本地文件。但是有没有一种方法可以在另一个浏览器窗口中显示 XML 内容,如
Window.Open(xmlString, . .. );
that would work the same as -
这将与 -
Window.Open(URL, . . .);
- I cannot use server-side language.
- I can use javaScript \ jQuery. (I already use them to create the XML)
- I can have a template XML file, near my HTML. Is there a way to display the template file and change its content ? almost the same as window.open: is it possible open a new window with modify its DOMor How to write JavaScript to a separate window?but I need to change XML nodes, and not HTML.
- 我不能使用服务器端语言。
- 我可以使用 javaScript\jQuery。(我已经使用它们来创建 XML)
- 我可以有一个模板 XML 文件,靠近我的 HTML。有没有办法显示模板文件并更改其内容?几乎与window.open相同:是否可以打开一个新窗口并修改其 DOM或如何将 JavaScript 写入单独的窗口?但我需要更改 XML 节点,而不是 HTML。
EDIT 1: try using myXmlWindow.document.write(xmlString)
编辑 1:尝试使用 myXmlWindow.document.write(xmlString)
=> I tried the suggested code -
=> 我尝试了建议的代码 -
var xmlString = xml2Str(xmlDocument);
myXmlWindow = window.open();
myXmlWindow.document.write(xmlString);
myXmlWindow.focus();
but it does not display the whole XML content, just the intern node values. and the new window still display "Connecting..." as it did not finish loading the content (missing close tag ???)
但它不显示整个 XML 内容,只显示内部节点值。并且新窗口仍然显示“正在连接...”,因为它没有完成加载内容(缺少关闭标签???)
maybe I need to tell it is XML content and not HTML ???
也许我需要告诉它是 XML 内容而不是 HTML ???
my xmlString :
我的 xmlString :
<root><device1>Name</device1><device2/><device3><Temprature_1>23.5</Temprature_1><Temprature_2>23.4</Temprature_2><Temprature_3>23.4</Temprature_3><Temprature_4>23.3</Temprature_4><Temprature_5>23.2</Temprature_5></device3></root>
the displayed content:
显示的内容:
Name23.523.423.423.323.2
EDIT 2: my code -
编辑 2:我的代码 -
function xml2Str(xmlNode) {
try {
// Gecko- and Webkit-based browsers (Firefox, Chrome), Opera.
return (new XMLSerializer()).serializeToString(xmlNode);
}
catch (e) {
try {
// Internet Explorer.
return xmlNode.xml;
}
catch (e) {
//Other browsers without XML Serializer
// alert('Xmlserializer not supported');
return('Xmlserializer not supported');
}
}
return false;
}
function fShow_xml_in_win() {
var xmlDocument = $.parseXML("<root/>");
var dev1 = xmlDocument.createElement('device1');
var dev2 = xmlDocument.createElement('device2');
var dev3 = xmlDocument.createElement('device3');
dev1.appendChild(xmlDocument.createTextNode('Name'));
xmlDocument.documentElement.appendChild(dev1);
xmlDocument.documentElement.appendChild(dev2);
xmlDocument.documentElement.appendChild(dev3);
var i;
var xNode;
for (i = 0; i < 5; i++) {
xNode = xmlDocument.createElement('Temprature_' + (i+1));
xNode.appendChild(xmlDocument.createTextNode( "myVal " + ((i+1) * 10) ));
dev3.appendChild(xNode);
}
var xmlString = xml2Str(xmlDocument);
alert(xmlString);
xmlString = "<?xml version='1.0' ?>" + xmlString; // I do not know how to add this node using parseXML :(
alert(xmlString);
myXmlWindow = window.open();
myXmlWindow.document.write(xmlString);
myXmlWindow.document.close(); // !! EDIT 3
myXmlWindow.focus();
return false;
}
EDIT 3: solved the "connecting..." problem
编辑 3:解决了“连接...”问题
I just needed to add myXmlWindow.document.close();
我只需要添加 myXmlWindow.document.close();
回答by SteveP
You can open a blank window and then write content to it as follows:
您可以打开一个空白窗口,然后按如下方式向其中写入内容:
myWindow=window.open('','','width=200,height=100')
myWindow.document.write(xmlString);
myWindow.focus()
You may need to do some work to format your xmlString, but I think this approach will do what you want. If your xmlString is formatted, try adding:
您可能需要做一些工作来格式化您的 xmlString,但我认为这种方法可以满足您的需求。如果您的 xmlString 已格式化,请尝试添加:
<?xml version="1.0" ?>
to the start of your string.
到字符串的开头。
回答by HILARUDEEN S ALLAUDEEN
My understanding from your post, are
我对你的帖子的理解是
1.(From your firts point) you get xml from somewhere which is not your control. My suggestion is why don't you get as JSON?
1.(从你的第一点来看)你从某个不是你控制的地方得到 xml。我的建议是你为什么不得到 JSON?
2.(From your second point) If those XML is created by you means, Why aren't you try to write those XML from reference? For example:
2.(从你的第二点开始)如果这些 XML 是由你创建的,你为什么不尝试从引用中编写这些 XML?例如:
var reference = window.open();
reference.document.write(<some string goes here>)
3.(From your third point) As per understanding from your second point. You can create xml. So why are you changing after write the document?
3.(从你的第三点)根据你的第二点的理解。您可以创建 xml。那你写完文档为什么要改呢?
Note: Generally XML is used for Server-to-server communication, JSON is used for Server-to-client(browser) communication.
注意:一般 XML 用于服务器到服务器的通信,JSON 用于服务器到客户端(浏览器)的通信。