This page contains the XML for <SCRIPT> DOMCharacterData object documentation.
Return to W3C DOM Parser Documentation Main Page
NOTE: DOMCharacterData descends from
DOMNode and inherits
all of the properties and methods of the DOMNode object
-
CONTENTS
-
DOMCharacterData - object description
-
-
-
Native Methods:
-
DOMCharacterData - getData method
-
DOMCharacterData - setData method
-
DOMCharacterData - getLength method
-
DOMCharacterData - substringData method
-
DOMCharacterData - appendData method
-
DOMCharacterData - insertData method
-
DOMCharacterData - deleteData method
-
DOMCharacterData - replaceData method
-
-
-
Inherited Methods:
-
DOMNode - getNodeName method
-
DOMNode - getNodeValue method
-
DOMNode - setNodeValue method
-
DOMNode - getNodeType method
-
DOMNode - getParentNode method
-
DOMNode - getChildNodes method
-
DOMNode - getFirstChild method
-
DOMNode - getLastChild method
-
DOMNode - getPreviousSibling method
-
DOMNode - getNextSibling method
-
DOMNode - getAttributes method
-
DOMNode - getNamespaceURI method
-
DOMNode - getPrefix method
-
DOMNode - getLocalName method
-
DOMNode - getOwnerDocument method
-
DOMNode - insertBefore method
-
DOMNode - replaceChild method
-
DOMNode - removeChild method
-
DOMNode - appendChild method
-
DOMNode - hasChildNodes method
-
DOMNode - cloneNode method
-
DOMNode - getElementsByTagName method
-
DOMNode - getElementsByTagNameNS method
-
DOMNode - setPrefix method
-
DOMNode - normalize method
-
DOMNode - isSupported method
-
DOMNode - getXML method
-
DOMNode - hasAttributes method
DOMCharacterData - object description
The DOMCharacterData interface extends DOMNode with a set of
attributes and methods for accessing character data in the DOM.
No DOM objects correspond directly to DOMCharacterData, though
DOMText and others do inherit the interface from it.
All offsets in this interface start from 0.
DOMCharacterData - getData method
W3C DOM Level 1
characterNode.getData();
- or -
characterNode.data
accepts:
N/A
returns:
String - the character data of the queried node
throws:
N/A
The getData method returns the character data of the queried node. Calling getData
is equivalent to calling getNodeValue for nodes that descend from DOMCharacterData.
NOTE: It is also possible to access the
data value of a DOMCharacterData node by accessing the
data property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//both of the following should be "Hello World"
alert(textNode.getData());
alert(textNode.getNodeValue());
}// end function xmljsDOMExample
DOMCharacterData - setData method
W3C DOM Level 1
characterNode.setData(<newValue>);
accepts:
String newValue - new value for the DOMCharacterData node
returns:
N/A
throws:
The setData method sets the value of the character data in the node. Calling setData
is equivalent to calling setNodeValue for nodes that descend from DOMCharacterData.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "<!--Hello World-->"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" comment
var commentNode = tag1.getFirstChild();
//The following should be "Hello World"
alert(commentNode.getData());
//now set the data
commentNode.setData("New Comment Data");
//the following should be
//"New Comment Data"
alert(commentNode.getData());
//The following should be
//<TAG1>
//<!--New Comment Data-->
//</TAG1>
alert(tag1.getXML());
}// end function xmljsDOMExample
DOMCharacterData - getLength method
W3C DOM Level 1
characterNode.getLength();
accepts:
N/A
returns:
int - the length of the character data in the queried node
throws:
N/A
The getLength method returns the length of the character data in the queried node.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "0123456789"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//The following should be "10"
alert(textNode.getLength());
}// end function xmljsDOMExample
DOMCharacterData - substringData method
W3C DOM Level 1
characterNode.substringData(<offset>, <count>);
accepts:
int offset - zero based offset to start the returned substring
int count - length of string to return
returns:
String - specified substring of the queried node's data
throws:
The substringData method returns a substring of the node's data value. The offset
is zero based.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//The following should be "Hello"
alert(textNode.substringData(0, 5));
//The following should be "World"
alert(textNode.substringData(6, 5));
}// end function xmljsDOMExample
DOMCharacterData - appendData method
W3C DOM Level 1
characterNode.appendData(<newData>);
accepts:
String newData - String data to append to current data of the node
returns:
N/A
throws:
The appendData method appends the value of newData to the current value of the
node. Calling appendData("new data") on a node with a data value of "data=" would
yield "data=new data".
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//append "!" to the node's data
textNode.appendData("!");
//the following should be
//"Hello World!"
alert(textNode.getData());
}// end function xmljsDOMExample
DOMCharacterData - insertData method
W3C DOM Level 1
characterNode.insertData(<offset>,<newData>);
accepts:
int offset - zero based offset to begin inserting data
String newData - String data to insert into the current data of the node
returns:
N/A
throws:
The insertData method inserts the value of newData into the current value of the
node at the offset location. Offset is zero based. Calling insertData(4,"=")
on a node with a data value of "data value" would
yield "data= value".
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//insert "There " into the node's data
//between "Hello" and "World". Keep
//the space after "Hello" and include
//a space in the inserted data to
//keep the sentance structure correct
textNode.insertData(6, "There ");
//the following should be
//"Hello There World"
alert(textNode.getData());
}// end function xmljsDOMExample
DOMCharacterData - deleteData method
W3C DOM Level 1
characterNode.deleteData(<offset>,<count>);
accepts:
int offset - zero based offset to begin deleting data
int count - length of data to delete
returns:
N/A
throws:
The deleteData method begins deleting the node's data at the zero-based
offset and deletes the number of characters specified in count.
Calling deleteData(7,1) on a node with a data value of "Hello WWorld" would
set the node's data to be "Hello World".
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello World"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello World" text
var textNode = tag1.getFirstChild();
//remove "There " from the node's data
//(note the space being removed as well)
textNode.deleteData(5,6);
//the following should be "Hello"
alert(textNode.getData());
}// end function xmljsDOMExample
DOMCharacterData - replaceData method
W3C DOM Level 1
characterNode.replaceData(<offset>,<count>,<newData>);
accepts:
int offset - zero based offset to begin replacing data
int count - length of data to replace
String newData - String of new data that replaces the old data
returns:
N/A
throws:
The replaceData method replaces the node's data between the zero-based
offset and offset + count and replaces that data with the data specified in newData.
Calling ReplaceData(7,1,"W") on a node with a data value of "Hello Xorld" would
set the node's data to be "Hello World".
NOTE: the new data and the replaced data do not have
to be the same length.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOT>"
+ "<TAG1>"
+ "Hello Their"
+ "</TAG1>"
+ "</ROOT>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the document root
var docRoot = domDoc.getDocumentElement();
//get the TAG1 node
var tag1 = docRoot.getFirstChild();
//get the "Hello Their" text
var textNode = tag1.getFirstChild();
//replace "Their" from the node's data
//with "There" for proper grammar
textNode.replaceData(6, 5, "There");
//the following should be "Hello There"
alert(textNode.getData());
}// end function xmljsDOMExample