Edit This Code:
<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();

// Check if the last node is an element node
function get_lastchild(n) {
    var x = n.lastChild;
    while (x.nodeType != 1) {
        x = x.previousSibling;
    }
    return x;
}

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    // Get the last child node of the document
    var x = get_lastchild(xmlDoc);
    // Get the last child node of the root element
    var y = get_lastchild(xmlDoc.documentElement);
    document.getElementById("demo").innerHTML =
    "Nodename: " + x.nodeName +
    " (nodetype: " + x.nodeType + ")<br>" +
    "Nodename: " + y.nodeName +
    " (nodetype: " + y.nodeType + ")<br>";
}
</script>

</body>
</html>


Result:
Try it Yourself - © w3schools.com
Privacy Policy