2014-01-08 45 views
1

我有一個函數readXML,它將從給定的xml文件中讀取值,它將替換特定節點值,替換特定值後,同樣的事情必須反映在原始文件中(我的意思是books.xml文件),修改完後如何將內容保存到xml文件中。使用Javascript保存XML文件

例如: 替換節點的代碼。

function readXML() { 

     xmlDoc = loadXMLDoc("books.xml"); 

     x = xmlDoc.documentElement; 

     //create a book element, title element and a text node 
     newNode = xmlDoc.createElement("book"); 
     newTitle = xmlDoc.createElement("title"); 
     newText = xmlDoc.createTextNode("A Notebook"); 

     //add the text node to the title node, 
     newTitle.appendChild(newText); 
     //add the title node to the book node 
     newNode.appendChild(newTitle); 

     y = xmlDoc.getElementsByTagName("book")[1] 
     //replace the first book node with the new node 
     x.replaceChild(newNode, y); 

     z = xmlDoc.getElementsByTagName("title"); 
     for (i = 0; i < z.length; i++) { 
      alert(z[i].childNodes[0].nodeValue); 

     } 

     //Here i have to save the xmlDoc in my local system. 

    } 

    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。

+0

如果你想將其保存在服務器端下載它,你將不得不上傳操作文件,並將其與服務器端邏輯存儲。 – Bergi

回答

1

可以使用FileSaver

var blob = new Blob([xmpString], {type: "text/xml"}); 

saveAs(blob, "test.xmp");