首页 > javascript > 使用javascript在新窗口中打开XML

使用javascript在新窗口中打开XML (Open XML in new window with javascript)

问题

我希望能够使用任何浏览器的本机xml格式查看xml数据。与在浏览器中打开本地xml文件类似。

  1. xml数据存储为javascript可以访问的字符串。
  2. 除了xml数据之外,我不需要网页上的任何其他内容。

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

我已经广泛搜索过这个问题的解决方案......我对使用XSLT或任何“自动滚动”格式化函数不感兴趣,因为我只想利用浏览器的内置xml格式。

解决方法

这可以使用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

问题

I would like to be able to view xml data using any browser's native xml formatting. Similar to opening a local xml file in a browser.

  1. The xml data is stored as a string which javascript has access to.
  2. I do not need anything else on the web page other than the xml data.

    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.

解决方法

This is possible using the Blob APIs:

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