使用 Javascript 创建 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11946530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an XML file using Javascript
提问by user1597148
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:
我dbbackup.xml
在同一位置创建了 XML 文件,如下所示:
<sticky>
<record></record>
</sticky>
But after running my script the xml
file is not getting updated.
但是在运行我的脚本后,xml
文件没有得到更新。
回答by Polyov
Javascript cannot modify files on disk, it only runs for the client in the client's web browser.
Javascript 不能修改磁盘上的文件,它只能在客户端的 Web 浏览器中为客户端运行。
To actually write to and from files on a server, you have to use server-side languages and technologies, like PHP or ASP.
要真正在服务器上写入和写入文件,您必须使用服务器端语言和技术,如 PHP 或 ASP。
回答by MikeyKennethR
I made this - making XML at client side then using everyday praksis Mike
我做了这个 - 在客户端制作 XML 然后使用日常的 praksis Mike
function makeSlot() {
函数 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);
}
回答by MikeyKennethR
SomeKidWithHTML is right.
SomeKidWithHTML 是对的。
JavaScript is designed to only modify a file, in memory, that is loaded inside a browser framework.
JavaScript 旨在仅修改内存中加载到浏览器框架内的文件。
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.
将浏览器想象成一个沙箱,您的孩子(html、xml 等)可以在其中玩耍。只要 Johnny (xml) 在沙箱中玩耍,一切都很好。但是,如果允许约翰尼在沙箱之外玩游戏,想想网站可能对您的机器造成的破坏。
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 本身不可能永久影响本地机器上的文件。它只能在沙箱内运行(在本地,它可以调用 Java 或其他 API 来影响更改,但这是另一回事)。
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 只是客户端。如果您预计它会影响服务器,则只能通过回调服务器来实现。在服务器上,您将需要某种编程(asp.net、java、php、html 等)来接收和应答该呼叫并对其进行处理。
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.
JavaScript 本身非常强大……但只是在沙箱(浏览器)中。要使其影响该浏览器之外的任何其他内容,它必须依赖于其他已经就绪并准备好接收这些请求的程序。
And this is all in the name of security, mostly.
这主要是为了安全。
回答by Huang Zhu
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.
您可以从客户端的网页中收集数据并将它们发送到服务器(ajax),然后服务器将生成 xml 文件并将链接发送回该文件(ajax)。使用 javascript 使用服务器返回的链接生成下载链接。
This is the way I do to solve the problem in one of my project.
这是我在我的一个项目中解决问题的方法。