XML DOM substringData() Method

Definition and Usage

The substringData() method extracts a substring from the annotation node.

Syntax:

commentNode.substringData(start,length)
Parameter description
start Required. Specifies the position of the first character to be returned. This value starts from 0.
length Required. Specifies the number of characters in the substring to be returned.

return value

Returns a string containing from the Comment node start starting length a character.

description

This method returns from the Comment node from start starting length A character. This method is useful only when the number of characters in the text contained in the node is greater than the maximum character number that can be filled in by the JavaScript implementation of the browser. In this case, the JavaScript program cannot directly use the data attribute of the Comment node, but must use a shorter substring of the node text. In practical applications, this situation is unlikely to occur.

Example

The following code segment uses JavaScript functions loadXMLDoc() Put the XML file books_comment.xml Load xmlDoc, then return a substring from the first comment node ("Hardcover"):

xmlDoc=loadXMLDoc("books_comment.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==8)
  { 
  //Only process comment nodes
  y=x[i].substringData(10,9);
  document.write(x[i].data);
  document.write("<br />");
  } 
}

The output of the above code:

(Hardcover)

In this example, we use a loop and if statement to perform processing that is only for comment nodes. The node type of comment nodes is 8.

Related Pages

XML DOM Reference Manual:CharacterData.substringData()