XML DOM substringData() Method
Definition and Usage
substringData()
Method to get a string from a comment node.
Syntax
substringData(start,length)
Parameter | Description |
---|---|
start | Required. Specifies the position at which to start extracting characters. The starting position is zero. |
length | Required. Specifies the number of characters to extract. |
Example
The following code loads "books_comment.xml" into xmlDoc and retrieves the string "(Hardcover)" from the first comment element:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books_comment.xml", true); xhttp.send(); function myFunction(xml) { var x, i, y, txt, xmlDoc; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book")[0].childNodes; for (i = 0; i < x.length; i++) { // Only handle comment nodes if (x[i].nodeType == 8) { y = x[i].substringData(33, 11); txt += y + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
In the above example, we used a loop and if test statements to ensure that we only handle comment nodes. The node type of comment nodes is 8.