使用Javascript创建XML文件
我有以下代码:使用Javascript创建XML文件,javascript,xml,Javascript,Xml,我有以下代码: 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
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;
}
但是在运行我的脚本后,
xml
文件不会得到更新。Javascript无法修改磁盘上的文件,它只在客户端的web浏览器中为客户端运行
要真正在服务器上写文件和写文件,必须使用服务器端语言和技术,如PHP或ASP。我做了这个-在客户端生成XML,然后使用日常praksis 迈克 函数makeSlot(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4&&xmlhttp.status==200)showBon();}
open(“POST”,“crMakeSlot.php”,true);
send(wrapUp());
}
/***
*使用XML生成最终事务
*/
函数wrapUp(){
var事务=document.implementation.createDocument(“,”,null);
var操作符=document.createElement(“操作符”);
var textblok1=document.createTextNode(document.getElementById(“rText”).value);
operator.appendChild(textblok1);
var root=document.createElement(“事务”);
setAttribute(“tstamp”,现在);
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(“凭证”,document.getElementById(“zVoucher”).value);
root.appendChild(操作符);
var divObj=document.getElementsByTagName(“div”);
/***
*当列值为4时,我们就完成了数据-下一个周期
*/
对于(ix=0;ix
SomeKidWithHTML是正确的
JavaScript被设计为只修改内存中加载在浏览器框架内的文件
把浏览器想象成孩子(html、xml等)可以玩的沙盒。只要Johnny(xml)在沙箱中玩,一切都很好。但如果约翰尼被允许在沙箱之外玩,想想网站可能会对你的机器造成的破坏
JavaScript本身无法永久影响本地计算机上的文件。它只能在沙箱中运行(在本地,它可以调用Java或其他API来影响更改,但这完全是另一回事)
JavaScript只是客户端。如果您希望它影响服务器,那么它只能通过调用服务器来实现。在服务器上,您需要某种编程(asp.net、java、php、html等)来接收和应答该调用,并对其进行处理
JavaScript本身非常强大。。。但只有在沙盒(浏览器)中。要使它影响该浏览器之外的任何其他内容,它必须依赖于已经就位并准备好接收这些请求的其他程序
这主要是出于安全的考虑。您可以在客户端从web页面收集数据,并将其发送到服务器(ajax),然后服务器将生成xml文件并发送回该文件的链接(ajax)。使用javascript使用服务器返回的链接生成下载链接
这就是我在我的一个项目中解决问题的方法。有没有其他方法可以使用javascript创建xml文件而不使用服务器端脚本?没有。如果没有服务器端语言,javascript无法与服务器交互。AJAX要求交付文件,但仅此而已。关于SomekidwithHTML对AJAX的评论,您可以使用JavaScript创建XML,然后使用类似于jQuery AJAX的东西将此信息发布回控制器方法。然后,您可以使用控制器保存到磁盘/数据库/任何位置。我来自MVC.NET背景(这里是示例实现),但在大多数web语言/框架中应该可以实现相同的模式。我想我应该详细阐述一下这一点,因为这是一种相当常见的模式。作为一个小的补充,如果您喜欢使用JavaScript,您可能会对一些JavaScript的服务器端实现感兴趣,例如node.js(请参阅)。
<sticky>
<record></record>
</sticky>
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);
}