Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open XML in new window with javascript

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.

like image 768
Will Knight Avatar asked Mar 12 '26 09:03

Will Knight


1 Answers

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
like image 112
Joshua Shaffer Avatar answered Mar 13 '26 23:03

Joshua Shaffer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!