如何从 xmlDoc 打印 xml 字符串

How to print xml string from xmlDoc

本文关键字:xml 字符串 打印 xmlDoc      更新时间:2023-09-26

我正在做的是从服务器读取xml文档,我必须在本地对该xml文档进行修改并将xml文档响应存储在服务器上,但我无法打印更改的xml响应请帮助我!!!

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

function stringsxmlparsing()
{
console.log("'n'nThis is books.xml demo'n'n");
xmlDoc=loadXMLDoc("books.xml");
//Changing attribute value
x=xmlDoc.getElementsByTagName('string');
x[0].setAttribute("name","food");
//Accessing attribute value
txt=xmlDoc.getElementsByTagName("string")[0].getAttribute("name");
console.log("getAttribute value :"+txt
 //here i have to send response xml to server
 //I have tried  
 //console.log(xmlDoc.toXMLString()) but won't worked
}

您需要等待响应,因为 XHR 请求是异步的。

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        parseResponse(xmlhttp.responseXML);
    }
}

源:
http://www.w3schools.com/json/json_http.asphttps://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest