Atara
Atara

Reputation: 3569

Open XML content in another window, using JavaScript

I understand I cannot save XML content to a local file, because of security restrictions. but is there a way I can show the XML content in another browser window, as

Window.Open(xmlString, . .. );

that would work the same as -

Window.Open(URL, . . .);

EDIT 1: try using myXmlWindow.document.write(xmlString)

=> I tried the suggested code -

    var xmlString = xml2Str(xmlDocument);
    myXmlWindow = window.open();
    myXmlWindow.document.write(xmlString);
    myXmlWindow.focus();

but it does not display the whole XML content, just the intern node values. and the new window still display "Connecting..." as it did not finish loading the content (missing close tag ???)

maybe I need to tell it is XML content and not HTML ???

my xmlString :

<root><device1>Name</device1><device2/><device3><Temprature_1>23.5</Temprature_1><Temprature_2>23.4</Temprature_2><Temprature_3>23.4</Temprature_3><Temprature_4>23.3</Temprature_4><Temprature_5>23.2</Temprature_5></device3></root>

the displayed content:

Name23.523.423.423.323.2

EDIT 2: my code -

function xml2Str(xmlNode) {
   try {
      // Gecko- and Webkit-based browsers (Firefox, Chrome), Opera.
      return (new XMLSerializer()).serializeToString(xmlNode);
  }
  catch (e) {
     try {
        // Internet Explorer.
        return xmlNode.xml;
     }
     catch (e) {
        //Other browsers without XML Serializer
        // alert('Xmlserializer not supported');
        return('Xmlserializer not supported');
     }
   }
   return false;
}


    function fShow_xml_in_win() {

        var xmlDocument = $.parseXML("<root/>");
        var dev1 = xmlDocument.createElement('device1');
        var dev2 = xmlDocument.createElement('device2');
        var dev3 = xmlDocument.createElement('device3');
        dev1.appendChild(xmlDocument.createTextNode('Name'));
        xmlDocument.documentElement.appendChild(dev1);
        xmlDocument.documentElement.appendChild(dev2);
        xmlDocument.documentElement.appendChild(dev3);

            var i;
            var xNode;
            for (i = 0; i < 5; i++) {
              xNode = xmlDocument.createElement('Temprature_' + (i+1));
              xNode.appendChild(xmlDocument.createTextNode( "myVal " + ((i+1) * 10) ));
              dev3.appendChild(xNode);
            }

        var xmlString = xml2Str(xmlDocument);

        alert(xmlString);

        xmlString = "<?xml version='1.0' ?>" + xmlString;  // I do not know how to add this node using parseXML :(
        alert(xmlString);

        myXmlWindow = window.open();
        myXmlWindow.document.write(xmlString);
        myXmlWindow.document.close();  // !! EDIT 3
        myXmlWindow.focus();

            return false;
    }

EDIT 3: solved the "connecting..." problem

I just needed to add myXmlWindow.document.close();

Upvotes: 3

Views: 12490

Answers (2)

SteveP
SteveP

Reputation: 19093

You can open a blank window and then write content to it as follows:

myWindow=window.open('','','width=200,height=100')
myWindow.document.write(xmlString);
myWindow.focus()

You may need to do some work to format your xmlString, but I think this approach will do what you want. If your xmlString is formatted, try adding:

<?xml version="1.0" ?>

to the start of your string.

Upvotes: 1

HILARUDEEN S ALLAUDEEN
HILARUDEEN S ALLAUDEEN

Reputation: 1752

My understanding from your post, are

1.(From your firts point) you get xml from somewhere which is not your control. My suggestion is why don't you get as JSON?

2.(From your second point) If those XML is created by you means, Why aren't you try to write those XML from reference? For example:

 var reference = window.open();
 reference.document.write(<some string goes here>)

3.(From your third point) As per understanding from your second point. You can create xml. So why are you changing after write the document?

Note: Generally XML is used for Server-to-server communication, JSON is used for Server-to-client(browser) communication.

Upvotes: 0

Related Questions