Using JavaScript(?) to save an XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dtbrown
    New Member
    • Oct 2007
    • 6

    #1

    Using JavaScript(?) to save an XML file

    I am working on a project that will locally create XML files. I have an HTML form that uses the Dojo Toolkit and XSLT to create the XML object. Currently I put the XML into a textarea. The used must then copy the XML and paste it into an XML file. I would like for this step to be skipped (i.e. have the XML file created/saved without copying and pasting.)

    My idea was to use JavaScript to open a new window, write the XML to that window, and then the user could save the page as an XML file, but the problem with that is either a) the XML tags are not rendered in the new window, or b) when the new window is saved it includes HTML tags.

    Does anyone know how I can save the XML to an XML file? Ideally I would just have a button on the original page that would create and save it, but for security reasons I don't think the browser will allow this. Two important constraints are 1) all of the code must be client-side, and 2) the page must work in IE and FireFox. Any ideas?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Unfortunately, this is not going to work with standard JavaScript.

    Is this for a restricted user environment where you could decide which browsers the users use?

    Comment

    • dtbrown
      New Member
      • Oct 2007
      • 6

      #3
      Originally posted by acoder
      Unfortunately, this is not going to work with standard JavaScript.

      Is this for a restricted user environment where you could decide which browsers the users use?
      Not really. The idea behind the project was that it would be cross-platform/cross-browser compatible, which rules out Active-x and FF extensions.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by dtbrown
        Two important constraints are 1) all of the code must be client-side, and 2) the page must work in IE and FireFox. Any ideas?
        Why must all the code be client-side?

        Comment

        • dtbrown
          New Member
          • Oct 2007
          • 6

          #5
          Originally posted by acoder
          Why must all the code be client-side?
          That is just one of the requirements that I have been given. As of right now the all of the HTML, JS, XSL, etc. files will be distributed to the users instead of being hosted on a site. Sounds kinda odd, but that's what they want.

          I just don't understand why I can open an XML document in a browser, but I can't write XML tags to a child window and save the contents of that window as an XML file. That just doesn't make any sense.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            hi ...

            you can write to a document in a browser window, but you cannot save anything (may be you could use the save page as menupotion?)... besides a cookie. this does make sense ... imagine a webpage that is able to save things at a client-machine ... may be malicious things ... nobody really wants that ... its a simple security issue. with (standard) javascript you cannot and you should not break out of the browser ...

            kind regards

            Comment

            • dtbrown
              New Member
              • Oct 2007
              • 6

              #7
              Originally posted by gits
              hi ...

              you can write to a document in a browser window, but you cannot save anything (may be you could use the save page as menupotion?)... besides a cookie. this does make sense ... imagine a webpage that is able to save things at a client-machine ... may be malicious things ... nobody really wants that ... its a simple security issue. with (standard) javascript you cannot and you should not break out of the browser ...

              kind regards
              Oh, I completely understand the issue with automatically saving something; I intended to require the users to use the Save Page As menu option. The problem I am having is when I write XML to the child window the XML tags do not display. Here are two methods that I have tried in JS:

              Code:
              function test(){
                  mywin = window.open('text/xml','', 'menubar=1,scrollbars=2,resizeable=1');
                  mywin.document.open;
                  mywin.opener = self;
                  XML_txt = '<tool attrib="now">text inside XML "tool" tag</tool>';
                  mywin.document.write(XML_txt);
                  }

              Code:
               var childWin = null;
               function launchChildW() {
                 if (childWin != null) childWin.window.close();
                   
                 wopts  = 'width=300,height=500,scrollbars=1';
                 childWin = window.open('', 'childW', wopts);
              
                 if (childWin != null) {
                   childWin.document.open()
                   if (childWin.opener == null) {
                     childWin.opener = self;
                   }
                 } else {
                     alert("Failed to open child window");
                 }
                 XML_txt = '<tool attrib="now">text inside XML "tool" tag</tool>';
                 childWin.document.writeln(XML_txt);
               }
              Both of those functions will open a new window. The text within the XML tags gets displayed, but the XML tags do not. The other problem is, when I go to 'Save As' the new window, it saves all the code from the original page - not what is in the new window.

              By the way, thanks everyone who has replied to these posts.

              Comment

              • nate damn

                #8
                the browser is intepreting your xml as tags you need to escape them using html entities &lt;tool ...&gt; i think that will help with showing the xml tags.

                Comment

                Working...