14218

Using window.open, document.open, and document.write to display XML (XML rendering gone)

This is related to another question, but is <strong>not</strong> a duplicate. It deals with a proposed solution that I have reached an impasse.

I have the following code that reads an XML, makes changes, opens a window, and writes the XML into the document. The problem is that the content is not rendered as XML. Any way to set a content type, etc, to have the browser handle the content as XML?

<script> var wxml; var xDoc; var xDevices, xInputs; var xDevice, xInput; function fSetXmlAInput(iDevice, iNode, nodeNewVal) { xInput = xInputs[iNode]; xValue = xInput.getElementsByTagName("value")[0]; // change node value: // console.log("nodeVal: " + xValue.firstChild.nodeValue); xValue.firstChild.nodeValue = nodeNewVal; // console.log("newVal: " + xValue.firstChild.nodeValue); } function fSetXmlDevice(iDevice) { xDevice = xDevices[iDevice]; xInputs = xDevice.getElementsByTagName("input"); fSetXmlAInput(iDevice, 0, "22"); fSetXmlAInput(iDevice, 1, "33"); } function alternativeLoadXML3() { // load xml file if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); } else { // IE 5/6 xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET", "my_template.xml", false); xhttp.send(); xDoc = xhttp.responseXML; xDevices = xDoc.getElementsByTagName("device"); fSetXmlDevice(1); var xmlText = serializeXmlNode(xDoc); var newWindow = window.open("my_template.xml", "Test", "width=300,height=300,scrollbars=1,resizable=1"); newWindow.document.open(); newWindow.document.write(xmlText); newWindow.document.close() }; </script>

Below the XML as well:

<?xml version="1.0" encoding="utf-8"?> <myXmlRoot> <device> <input><name>"name 1"</name><value>{replaceMe!}</value></input> <input><name>"name 2"</name><value>{replaceMe!}</value></input> </device> <device> <input><name>"name 1"</name><value>{replaceMe!}</value></input> <input><name>"name 2"</name><value>{replaceMe!}</value></input> </device> <device> <input><name>"name 1"</name><value>{replaceMe!}</value></input> <input><name>"name 2"</name><value>{replaceMe!}</value></input> </device> </myXmlRoot>

Any way to force the browser to render content in new window as XML...or does using document.open and document.write mean code is rendered as HTML?

Related: Change XML content using JavaScript, missing refresh

Answer1:

Both document.open and document.write are used to write HTML or Javascript to a document. This goes back to the DOM Level 2 Specification of document.open, which assumes that the document is opened in order to write unparsed HTML.

Instead of using document.open and document.write, I would instead suggest dymanically adding elements using the XML DOM as in sampleElement.appendChild(XMLnode)

A similar question can also be found here: how to display xml in javascript?

Answer2:

Use dataURI to write xml into new window is very easy.

window.open('data:text/xml,'+encodeURIComponent(xmlText),
     "Test", "width=300,height=300,scrollbars=1,resizable=1");

    

Recommend

  • iPhone: programmatically check if vibration is enabled
  • ld: library not found for -lGoogleToolboxForMac
  • historyserver not able to read log after enabling kerberos
  • How to check in linux kernel at vfs layer whether the file object is for a directory or a file
  • How to add a d3js graph in slideshow instead of body?
  • How can MPAndroidChart begin at the last x-value with a large dataset?
  • iOS - Custom keyboard, updating text field in parent view
  • Use reorder in ggplot2 wrapped in a function
  • Why use hashing to create pathnames for large collections of files?
  • Dynamic timeseries C3js chart
  • USB data transfer to iPhone
  • How can I get karma browser's to use a dark theme?
  • I can't grab specific URL in search page
  • Using window.open, document.open, and document.write to display XML (XML rendering gone)
  • How Can I Replace a Script Element?
  • Find and append hrefs of a certain class
  • How to remove attributes using PHP DOMDocument?
  • Unmarshaling of soap response with namespace prefix
  • php echo first divs
  • document.getElementsByTagName(“a”) misses a link
  • SCRIPT tag removed from AJAX response in Internet Explorer
  • fb-video change url and reload player at runtime (without page refresh)
  • hashmap in android xml parsing
  • JavaScript RegEx to match punctuation NOT part of any HTML tags
  • Knowing if a previous handler for an event returned false. defaultPrevented in IE < 9?
  • Parse XML string and build a list of strings
  • Select element inside WebView and get details
  • serialize list of huge composite graphs using protobuf-net causing out-of-memory-exception
  • Javascript get anchor href on click
  • Dom Node for PHP find href attribute issue
  • getElementsByClassName and AJAX
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Simple linked list-C
  • CakePHP ACL tutorial initDB function warnings
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How can i traverse a binary tree from right to left in java?