gpt4 book ai didi

javascript - 如何在 Mozilla 中将 E4X XML 原语写入新的文本/xml 窗口?

转载 作者:行者123 更新时间:2023-12-02 20:15:06 28 4
gpt4 key购买 nike

我正在编写一个小型浏览器端 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,因此所有标签自然都会呈现错误。 From this fixed bug看来 document.open 创建了正确的 contentType,但 document.write 搞砸了。

  • 这只需要在 Firefox 2+ 中运行。
  • 服务器端不是一个选项。
  • 我选择了 Javascript 和 Mozilla,因为 ECMAScript 对 XML 支持。

最佳答案

如果您只是尝试使用 XML 打开新选项卡:

open("data:application/xml," + encodeURIComponent(xmlDoc.toXMLString()));

如果您希望用户能够下载 XML,请使用 application/octet-stream 而不是 application/xml。

关于javascript - 如何在 Mozilla 中将 E4X XML 原语写入新的文本/xml 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6408416/

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