<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this);
}
};
xhttp.open("GET", "/demo/xml/books.xml", true);xhttp.send();
function myFunction(xml) { var xmlDoc = xml.responseXML;
var x, y, i, newEle, newText, txt;
newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第一版"); newEle.appendChild(newText);
x = xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newEle);
xlen = x.childNodes.length;
y = x.firstChild;
txt = "";
for (i = 0; i < xlen; i++) { if (y.nodeType == 1) { txt += y.nodeName + "<br>";
}
y = y.nextSibling;
}
document.getElementById("demo").innerHTML = txt;}
</script>
</body>
</html>