如何在 Mozilla 中将 E4X XML 原语写入新的文本/xml 窗口?
我正在编写一个小型浏览器端 Javascript,用于从文件中解析 XML 并对其进行修改。理想情况下,输出是一个 .xml 文件,但当然,让 JS 客户端写入文件几乎是不可能的,所以我放弃了。
我希望 Firefox 打开一个 contentType text/xml 的新窗口,其中包含我的序列化 xml:
var xmlDoc = new XML(SourceXMLString); //SourceXMLString is read from an xml text file
output = window.open("");
output.document.open("text/plain");
output.document.write(xmlDoc.toXMLString());
output.document.close();
但是,任何 document.write 实例似乎都将 contentType 设置为 text/html,因此所有标签自然都会呈现错误。 从这个已修复的错误看来 document.open 创建了正确的 contentType 但 document.写得乱七八糟。
- 这只需要在 Firefox 2+ 中工作。
- 服务器端不是一个选项。
- 我选择了 Javascript & Mozilla 因为 ECMAScript 对 XML 支持。
I'm writing a small browser-side Javascript that parses XML from a file and modifies it. The output ideally would be a .xml file, but of course making a JS client write files is nearly impossible so I gave up on that.
I want Firefox to open a new window of contentType text/xml, with my serialized xml in it:
var xmlDoc = new XML(SourceXMLString); //SourceXMLString is read from an xml text file
output = window.open("");
output.document.open("text/plain");
output.document.write(xmlDoc.toXMLString());
output.document.close();
However any document.write instance seems to set the contentType to text/html and so all the tags are rendered wrong, naturally. From this fixed bug it seems that document.open creates the right contentType but document.write messes it up.
- This only needs to work in Firefox 2+.
- Server-side is not an option.
- I went with Javascript & Mozilla because of ECMAScript for XML support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是尝试使用 XML 打开新选项卡:
如果您希望用户能够下载 XML,请使用 application/octet-stream 而不是 application/xml。
If you're just trying to open a new tab with the XML:
If you want the user to be able to download the XML, use application/octet-stream instead of application/xml.