简体   繁体   English

使用 javascript 在新窗口中打开 XML

[英]Open XML in new window with javascript

I would like to be able to view xml data using any browser's native xml formatting.我希望能够使用任何浏览器的本机 xml 格式查看 xml 数据。 Similar to opening a local xml file in a browser.类似于在浏览器中打开本地 xml 文件。

  1. The xml data is stored as a string which javascript has access to. xml 数据存储为 javascript 可以访问的字符串。
  2. I do not need anything else on the web page other than the xml data.除了 xml 数据之外,我不需要网页上的任何其他内容。

    var xmlString = document.getElementById("xmlDivContent" + name).innerText; window.open("data:text/xml;charset=utf-8," + xmlString, "", "_blank");

I've searched around, extensively, for a solution to this problem...I'm not interested in using XSLT or any "home-rolled" formatting function because I just want to take advantage of the browser's built-in xml formatting.我已经广泛搜索了这个问题的解决方案......我对使用 XSLT 或任何“家庭滚动”格式功能不感兴趣,因为我只想利用浏览器的内置 xml 格式。

This is possible using the Blob APIs:这可以使用 Blob API 实现:

let blob = new Blob(['<yourxmlstringhere></yourxmlstringhere>'], {type: 'text/xml'});
let url = URL.createObjectURL(blob);
window.open(url);
URL.revokeObjectURL(url); //Releases the resources

This used to be achievable by simply creating a data URL containing the encoded xml information.这过去可以通过简单地创建一个包含编码的 xml 信息的数据 URL 来实现。 Most browsers, notably Chrome, do not support this functionality in a ticket described here: Intent to Deprecate and Remove: Top-frame navigations to data URLs大多数浏览器,尤其是 Chrome,在此处描述的票证中不支持此功能: Intent to Deprecate and Remove: Top-frame navigations to data URLs

In this post they detail the possible alternatives as:在这篇文章中,他们详细说明了可能的替代方案:

  • Generate the file on the backend and send it to the user over http/https.在后端生成文件并通过 http/https 将其发送给用户。

  • Initiate a download instead of displaying the URL.启动下载而不是显示 URL。

  • If the contents of the URL is trusted, iframe the URL so that the omnibox displays the site's URL.如果 URL 的内容受信任,则对 URL 进行 iframe,以便多功能框显示站点的 URL。

I ended up downloading the xml.我最终下载了xml。 If someone could come up with a solution for displaying the XML contents in an iframe (using a browser's native xml formatting, that would be a nice improvement)如果有人能想出一个在 iframe 中显示 XML 内容的解决方案(使用浏览器的原生 xml 格式,那将是一个很好的改进)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM