如何从xmlDoc打印xml字符串

时间:2015-02-01 04:45:53

标签: javascript html xml

我正在从服务器读取xml文档,我必须在本地修改该xml文档并将xml文档响应存储在服务器上,但我无法打印更改的xml响应请帮帮我!!!! < / p>

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
}

1 个答案:

答案 0 :(得分:0)

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

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

资料来源:
http://www.w3schools.com/json/json_http.asp
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest