3

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.

2 Answers 2

10

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
Sign up to request clarification or add additional context in comments.

Comments

0

This used to be achievable by simply creating a data URL containing the encoded xml information. 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

In this post they detail the possible alternatives as:

  • Generate the file on the backend and send it to the user over http/https.

  • Initiate a download instead of displaying the URL.

  • If the contents of the URL is trusted, iframe the URL so that the omnibox displays the site's URL.

I ended up downloading the 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)

2 Comments

How do you dowloaded it?
@mautrok using javascript. I placed the xml on the page as plain text in a small (ish) div and used javascript to download that div.InnerText content into a file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.