XML DOM replaceData() Method

Definition and Usage

The replaceData() method uses the specified string to replace the data in the comment node.

Syntax:

commentNode.replaceData(start,length,string)
parameter description
start required. The position where the character is to be replaced. The value starts from 0.
length required. The number of characters to be replaced. It is
string required. The number of characters to be replaced by start and length The string of the specified character. It is

description

This method uses a string string replacement from start starting length characters. If start add length is greater than the length of the Comment node, then from start All characters starting will be replaced.

Example

The following code block uses a JavaScript function loadXMLDoc() Load the XML file books_comment.xml Load xmlDoc and then replace "Hardcover" in the comment node of the first <book> element with "Paperback":

xmlDoc=loadXMLDoc("books_comment.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==8)
  { 
  //Process Only Comment Nodes
  x[i].replaceData(10,9,"Easy");
  document.write(x[i].data);
  document.write("<br />");
  } 
}

The output of the above code:

(Book 6) (Paperback)

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

Related Pages

XML DOM Reference Manual:CharacterData.replaceData()