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

发布于 2024-11-16 03:21:21 字数 764 浏览 7 评论 0原文

我正在编写一个小型浏览器端 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-11-23 03:21:21

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

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

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

If you're just trying to open a new tab with the XML:

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

If you want the user to be able to download the XML, use application/octet-stream instead of application/xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文