Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an XML file using Javascript

Tags:

javascript

xml

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.

like image 700
user1597148 Avatar asked Sep 30 '26 01:09

user1597148


2 Answers

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.

like image 162
Polyov Avatar answered Oct 01 '26 15:10

Polyov


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);
}
like image 20
MikeyKennethR Avatar answered Oct 01 '26 15:10

MikeyKennethR



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!