我有函数readXML,它将从给定的xml文件读取值并且它将替换特定的节点值,在替换特定值后,同样的事情必须反映原始文件(我的意思是books.xml文件),之后做修改如何在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 :(得分:1)
您可以使用FileSaver
下载var blob = new Blob([xmpString], {type: "text/xml"});
saveAs(blob, "test.xmp");