最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Open XML content in another window, using JavaScript - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Mar 12, 2013 at 9:17 AtaraAtara 3,5696 gold badges41 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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.

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 munication, JSON is used for Server-to-client(browser) munication.

发布评论

评论列表(0)

  1. 暂无评论