Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, . . .);
  • I cannot use server-side language.
  • I can use javaScript \ jQuery. (I already use them to create the XML)
  • I can have a template XML file, near my HTML. Is there a way to display the template file and change its content ? almost the same as window.open: is it possible open a new window with modify its DOM or How to write JavaScript to a separate window? but I need to change XML nodes, and not HTML.

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();

like image 826
Atara Avatar asked Aug 05 '26 04:08

Atara


1 Answers

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.

like image 80
SteveP Avatar answered Aug 07 '26 18:08

SteveP



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!