【问题标题】:Saving XML File using Javascript使用 Javascript 保存 XML 文件
【发布时间】:2017-09-24 15:50:02
【问题描述】:

我有 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 新手。

【问题讨论】:

  • 如果你想把它保存在服务器端,你必须上传处理过的文件并用服务器端逻辑存储它。

标签: javascript xml


【解决方案1】:

您可以使用FileSaver下载它

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

saveAs(blob, "test.xmp");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多