By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

Using JavaScript(?) to save an XML file

P: 6
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?
Oct 17 '07 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
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?
Oct 18 '07 #2

P: 6
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.
Oct 18 '07 #3

acoder
Expert Mod 15k+
P: 16,027
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?
Oct 18 '07 #4

P: 6
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.
Oct 19 '07 #5

gits
Expert Mod 5K+
P: 5,384
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
Oct 19 '07 #6

P: 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
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:

Expand|Select|Wrap|Line Numbers
  1. function test(){
  2.     mywin = window.open('text/xml','', 'menubar=1,scrollbars=2,resizeable=1');
  3.     mywin.document.open;
  4.     mywin.opener = self;
  5.     XML_txt = '<tool attrib="now">text inside XML "tool" tag</tool>';
  6.     mywin.document.write(XML_txt);
  7.     }        
  8.  

Expand|Select|Wrap|Line Numbers
  1.  var childWin = null;
  2.  function launchChildW() {
  3.    if (childWin != null) childWin.window.close();
  4.  
  5.    wopts  = 'width=300,height=500,scrollbars=1';
  6.    childWin = window.open('', 'childW', wopts);
  7.  
  8.    if (childWin != null) {
  9.      childWin.document.open()
  10.      if (childWin.opener == null) {
  11.        childWin.opener = self;
  12.      }
  13.    } else {
  14.        alert("Failed to open child window");
  15.    }
  16.    XML_txt = '<tool attrib="now">text inside XML "tool" tag</tool>';
  17.    childWin.document.writeln(XML_txt);
  18.  }
  19.  
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.
Oct 19 '07 #7

P: n/a
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.
Nov 6 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.