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

Creating an XML file using Javascript - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Nov 30, 2012 at 10:52 dda 6,2132 gold badges27 silver badges35 bronze badges asked Aug 14, 2012 at 5:58 user1597148user1597148 311 gold badge1 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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.

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 plete - 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);
}

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, 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.

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.

发布评论

评论列表(0)

  1. 暂无评论