问题描述
我目前正在使用以下代码来显示部分 DOM 中的 XML 代码.
I am currently using the following code to display the XML code from part of the DOM.
var sXML = new XMLSerializer().serializeToString(document.getElementsByTagName("TopElementOfBlockOfInterest")[0]);
var win;
var doc;
win = window.open("", "", "",false);
doc = win.document;
doc.open("text/plain");
doc.write(sXML);
doc.close();
我看到的是 XML 代码.但是,当我在 Firefox 20.0 上选择另存为"时,保存的是一个 html 文件.有没有办法以用户保存将 XML 代码保存到文件的形式显示 XML 字符串?现在的方式是,用户可以复制和粘贴,但我更喜欢更传统的保存操作.
What I see is XML code. However, when I choose "Save As" on Firefox 20.0, what is saved is an html file. Is there a way to display the XML string in a form in which the user save save the XML code to a file? The way it is now, the user can copy and paste but I would prefer a more conventional Save operation.
推荐答案
将其转换为数据 uri 然后打开
Convert it to a data uri and then open that
var uri = new XMLSerializer().serializeToString( // serialise
document.getElementsByTagName("TopElementOfBlockOfInterest")[0]
),
win;
uri = 'data:text/plain,' + window.encodeURIComponent(uri); // to data URI
win = window.open(uri, '_blank'); // open new window
SaveAs 现在将默认为 .txt
.您可能还希望使用 MIME text/xml
或 application/xml
来实际显示 XML 内容,但是在浏览器中查看时,这可能会呈现而不是显示为纯文本.
SaveAs will now default to .txt
. You may also wish to use MIME text/xml
or application/xml
as you're actually displaying XML content, however this might be rendered rather than displayed as plain text, when viewed in a browser.
这篇关于将 XMLSerializer().serializeToString 字符串输出到用户可保存文本窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!