2012-08-14 59 views
3

一個XML文件,我有以下代碼:創建使用Javascript

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

我已經在同一位置創建dbbackup.xml和XML文件的樣子:

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

但運行我的腳本後xml文件沒有得到更新。

回答

1

Javascript無法修改磁盤上的文件,只能在客戶端的Web瀏覽器中爲客戶端運行。

要實際寫入和從服務器上的文件寫入,必須使用服務器端語言和技術,如PHP或ASP。

+0

是否有任何其他方式使用JavaScript創建xml文件而不使用服務器端腳本? – user1597148 2012-08-14 06:10:00

+0

沒有。Javascript沒有能力與服務器端進行交互,沒有服務器端語言。 AJAX請求提交文件,但就是這樣。 – Polyov 2012-08-14 06:33:40

+0

就SomekidwithHTML有關AJAX的評論而言,您可以使用JavaScript創建XML,然後使用類似jQuery AJAX的東西將此信息發回給控制器方法。然後你可以使用控制器保存到磁盤/數據庫/不管。我來自MVC。NET背景(這裏的示例實現http://stackoverflow.com/a/8517361/201648),但應該可以在大多數Web語言/框架中實現相同的模式。我想我應該詳細說明這一點,因爲這是一個相當常見的模式。 – 2013-02-03 05:55:57

1

我做了這一點 - 在客戶端進行XML然後使用日常praksis 邁克

功能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); 
} 
1

SomeKidWithHTML是正確的。

JavaScript旨在僅修改在瀏覽器框架內加載的內存中的文件。

將瀏覽器視爲您的孩子(html,xml等)可以播放的沙箱。只要Johnny(xml)在沙箱中播放,一切都很好。但是如果Johnny被允許在沙箱外玩,只要想想網站可能會在你的機器上造成的破壞。

有沒有辦法JavaScript可以永久影響本地計算機上的文件,本身。它只能在沙盒內玩(本地,它可以調用Java或其他API來影響變化,但這是一個完整的其他處理)。

JavaScript僅限客戶端。如果你期望它影響服務器,它只能通過回調服務器來完成。在服務器上,你將需要一些編程(asp.net,java,php,html等)來接收和回答這個調用,並用它做一些事情。

JavaScript本身功能非常強大......但只能在沙盒(瀏覽器)中使用。爲了影響瀏覽器以外的其他任何內容,它必須依賴於已經存在的其他程序並準備好接收這些請求。

這大都是以安全爲名。

1

您可以從客戶端的網頁收集數據並將它們發送到服務器(ajax),然後服務器將生成xml文件併發送一個鏈接到文件(ajax)。使用JavaScript來使用服務器返回的鏈接生成下載鏈接。

這是我在我的一個項目中解決問題的方式。