使用 Javascript 保存 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20996242/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:47:23  来源:igfitidea点击:

Saving XML File using Javascript

javascriptxml

提问by Bilal

i have function readXML that will read the value from given xml file and it will replace specific node value , after replacing the specific value, same thing has to be reflected the raw file( I mean books.xml file), after doing the modification how to save the content in xml file.

我有函数 readXML 它将从给定的 xml 文件中读取值,它将替换特定的节点值,在替换特定值后,同样的事情必须反映到原始文件(我的意思是 books.xml 文件),在修改之后如何将内容保存在 xml 文件中。

Ex: Code for replacing node.

例如:替换节点的代码。

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

I am new to XML.

我是 XML 的新手。

回答by Blackrabbit99

You can download it using FileSaver

您可以使用FileSaver下载它

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

saveAs(blob, "test.xmp");