3

I have the following code:

xmlDoc=loadXMLDoc("dbbackup.xml");
x=xmlDoc.getElementsByTagName("record");
alert(x);
for (i=0;i<3;i++) {
  newel=xmlDoc.createElement("edition");
  newtext=xmlDoc.createTextNode("first");
  alert("x  : "+x[i]);
  alert("newtext :"+newtext.nodevalue);
  x[i].appendChild(newel);
  alert("sd");
}
function loadXMLDoc(dname) {
  if (window.XMLHttpRequest) {
    xhttp=new XMLHttpRequest();
  } else {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhttp.open("GET",dname,false);
  xhttp.send();
  return xhttp.responseXML;
}

I have created dbbackup.xml in the same location and the XML file looks like:

<sticky>
  <record></record>
</sticky>

But after running my script the xml file is not getting updated.

dda
  • 6,030
  • 2
  • 25
  • 34
user1597148
  • 31
  • 1
  • 1
  • 2

4 Answers4

1

Javascript cannot modify files on disk, it only runs for the client in the client's web browser.

To actually write to and from files on a server, you have to use server-side languages and technologies, like PHP or ASP.

Polyov
  • 2,281
  • 2
  • 26
  • 36
  • is there any other way to create a xml file using javascript without using server side scripting? – user1597148 Aug 14 '12 at 06:10
  • No. Javascript has no ability to interact with a server without server-side languages. AJAX requests files to be delivered, but that's it. – Polyov Aug 14 '12 at 06:33
  • Just with regard to SomekidwithHTML's comment about AJAX, you could create the XML with JavaScript, then using something like a jQuery AJAX to POST this information back to a controller method. You could then use the controller to save to disk/database/whatever. I come from an MVC.NET background (example implementation here http://stackoverflow.com/a/8517361/201648), but it should be possible to implement this same pattern in most web languages/frameworks. I thought I should elaborate on this point a little since this is a fairly common pattern. – Aaron Newton Feb 03 '13 at 05:55
  • As a slight side-track, if you're like working with JavaScript, there are some server-side implementations of JavaScript such as node.js which may be of interest to you (see http://nodejs.org/). – Aaron Newton Feb 03 '13 at 05:57
1

I made this - making XML at client side then using everyday praksis Mike

function makeSlot() {

  var xmlhttp = new XMLHttpRequest();    
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) showBon(); } 
  xmlhttp.open("POST","crMakeSlot.php",true);  
  xmlhttp.send(wrapUp());  
}

/***
* make the final transaction - using XML
*/
function wrapUp () {   

  var transaction = document.implementation.createDocument("","", null);           

  var operator = document.createElement("operator");
  var textblok1 = document.createTextNode(document.getElementById("rText").value);
      operator.appendChild(textblok1);         

  var root = document.createElement("transaction"); 
      root.setAttribute("tstamp",  now);
      root.setAttribute("sequenceno", zSequenceNo.textContent);
      if (parseInt(document.getElementById("zDankort").value) > 0) root.setAttribute("dankort", document.getElementById("zDankort").value);        
      if (parseInt(document.getElementById("zCash").value) > 0) root.setAttribute("cash", document.getElementById("zCash").value);              
      if (parseInt(document.getElementById("zCredit").value) > 0) root.setAttribute("credit", document.getElementById("zCredit").value);              
      if (parseInt(document.getElementById("zCheck").value) > 0) root.setAttribute("check", document.getElementById("zCheck").value);              
      if (parseInt(document.getElementById("zGiftcard").value) > 0) root.setAttribute("giftcard", document.getElementById("zGiftcard").value);              
      if (parseInt(document.getElementById("zVoucher").value) > 0) root.setAttribute("voucher", document.getElementById("zVoucher").value);              

      root.appendChild(operator);

  var divObj = document.getElementsByTagName("div");   

/***
*  when column value is 4, then we have our data complete - next cycle 
*/
  for (ix = 0; ix < divObj.length; ix++) {     
    switch (divObj[ix].getAttribute("column")) {
     case "1": var row = document.createElement("row"); row.setAttribute("item",divObj[ix].textContent);
     case "2": row.setAttribute("price",divObj[ix].textContent);        
     case "3": row.setAttribute("quantum",divObj[ix].textContent);        
     case "4": root.appendChild(row); 
     default: break;                 
    }
  }        
  transaction.appendChild(root);
  return(transaction);
}
MikeyKennethR
  • 600
  • 4
  • 16
1

SomeKidWithHTML is right.

JavaScript is designed to only modify a file, in memory, that is loaded inside a browser framework.

Think of the browser as a sandbox that your kids (html, xml, etc.) can play in. As long as Johnny (xml) is in the sandbox playing, all is well. But if Johnny were allowed to play outside of that sandbox, just think of the havoc that could be done on your machine by websites.

There is NO WAY a JavaScript can permanentally affect a file on your local machine, by itself. It can only play inside the sandbox (locally, it can make calls to Java, or an other API, to affect change, but that's a whole other deal).

JavaScript is client side only. If you expect it to affect a server, it can only do it through calls back to the server. At the server you will need some kind of programming (asp.net, java, php, html, others) to receive and answer that call and do something with it.

JavaScript, by itself, is very powerful... but only inisde the sandbox (browser). For it to affect anything else outside of that browser it must depend on other programs already in place and ready to receive those requests.

And this is all in the name of security, mostly.

1

You can collect data from the web page in client side and send them to the server (ajax), which will then generate the xml file and send back a link to the file (ajax). Use javascript to generate a download link using the link returned by the server.

This is the way I do to solve the problem in one of my project.

Huang Zhu
  • 21
  • 2