Atributo lastChild do XML DOM
definição e uso
último filho
This property returns the last child node of the selected element.
If the selected node has no child nodes, this property returns NULL.
Syntax
elementNode.lastChild
Note:Firefox and most other browsers will consider whitespace or newlines as text nodes, while Internet Explorer will not. Therefore, in the following example, we use a function to check the node type of the last child node.
The nodeType of element nodes is 1, so if the last child node is not an element node, it will move to the previous node and check if that node is an element node. This will continue until the last child node (which must be an element node) is found. This ensures that the result is correct in all browsers.
Tip:For more information on differences between browsers, please visit the DOM Browsers section in the XML DOM tutorial.
Example
The following code loads "books.xml" into xmlDoc and gets the last child node:var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); // Checks 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 x, i, txt, firstNode, xmlDoc; xmlDoc = xml.responseXML; x = xmlDoc.documentElement; txt = ""; firstNode = get_lastchild(x); for (i = 0; i < firstNode.childNodes.length; i++) { if (firstNode.childNodes[i].nodeType == 1) { // Only handles element nodes txt += firstNode.childNodes[i].nodeName +"}}" " = " + firstNode.childNodes[i].childNodes[0].nodeValue + "<br>"; } } document.getElementById("demo").innerHTML = txt; }