XML technologies / XML / XML in a Nutshell / DOM reference / Object reference / CharacterData
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
The interface CharacterData extends the interface Node with methods for manipulating text data. All object types in DOM, containing text data derive directly or indirectly from this interface.
This interface’s text-processing facilities are similar to those offered by most modern programming languages. As in C/C++ or Java all routines processing CharacterData always use zero-based indexing.
Java example
// Erzeuge einen neuen Textknoten
Text ndText = doc.createTextNode("Im Wein liegt Wahrheit.");
// Und wandle ihn durch einen Cast in ein CharacterData-Objekt um
CharacterData ndCD = (CharacterData)ndText;
Attributes
CharacterData objects have the following attributes:
data: DOMString
This attribute returns the raw text data of the CharacterData node. Since a particular DOM implementation limits the size of this text stored in the CharacterData node, you may need to use the method substringData to split the text into small pieces that do not cause implementation problems.
Exceptions
NO_MODIFICATION_ALLOWED_ERR
Raised if the
dataattribute is read-only for this DOM object type and an attempt is made to modify it.DOMSTRING_SIZE_ERR
Raised if the value being read is too long to fit in a
DOMStringtype in this implementation.
Java bindings
public String getData( ) throws DOMException;
public void setData(String data) throws DOMException;
Java example
// Zitiere den Inhalt des CharacterData-Knotens
CharacterData ndCD =
doc.createTextNode("Unquoted text.");
...
ndCD.setData('\"' + ndCD.getData( ) + '\"');
length: unsigned long
The length of the DOMString, stored in the data attribute. For all methods of this interface that expect an index parameter, valid values satisfy 0 <= index < length. Zero is valid because empty CharacterData nodes are allowed. Read-only.
Java binding
public long getLength( );
Java example
// Inhalt des CharacterData-Knotens ausgeben
CharacterData ndCD = (CharacterData)doc.createTextNode("Dieser String besteht aus
32 Zeichen.");
System.out.println("Der String \'" + ndCD.getData( ) + "\' enthält "
+ Long.toString(ndCD.getLength( )) + " Zeichen.");
Methods
The following methods are defined for CharacterData objects:
appendData: arg
The string passed in the parameter arg is appended to the value of the data attribute.
Argument
arg: DOMString
The string to append.
Exception
NO_MODIFICATION_ALLOWED_ERR
Raised when the node is read-only.
Java binding
public void appendData(String arg) throws DOMException;
Java example
// Ein String wird erweitert.
// Erzeuge einen Textknoten und behandle ihn als CharacterData-Objekt.
CharacterData ndCD = (CharacterData)doc.createTextNode("Die Wahrheit ist ");
// Werfen wir eine Münze
ndCD.appendData((Math.random( ) < 0.5) ? "da draußen." : "hier drin.");
System.out.println(ndCD.getData( ));
deleteData: offset, count
Removes a section from the DOMString of the data attribute. This method removes count characters, starting with the character at index offset.
Arguments
offset: unsigned long
The position in the
dataattribute from whichcountcharacters are removed.count: unsigned long
The number of characters to remove; if
offset+countis greater than or equal to the value of thelengthattribute, the rest of the string starting atoffsetis removed.
Exceptions
INDEX_SIZE_ERR
Raised if the
offsetparameter is too large, that is, greater than or equal to the length of theDOMString.NO_MODIFICATION_ALLOWED_ERR
Raised when the node is read-only.
Java binding
public void deleteData(long offset, long count)
throws DOMException;
Java example
// Erzeuge ein neues Textobjekt, und behandle es als CharacterData-Objekt.
CharacterData ndCD = doc.createTextNode("Die Wahrheit ist nicht da draußen.");
// Meinungsänderung ...
ndCD.deleteData(15, 6);
System.out.println(ndCD.getData( ));
insertData: offset, arg
This method takes a string, splits the current contents of the data attribute at position offset and inserts the string from the arg argument between the two parts.
Arguments
offset: unsigned long
The position at which to insert the string into the
dataattribute. Zero indicates the beginning.arg: DOMString
The string to insert.
Exceptions
INDEX_SIZE_ERR
Raised if the parameter
offsetis not a validdata DOMStringinserted at position 0.NO_MODIFICATION_ALLOWED_ERR
Raised when the node is read-only.
Java binding
public void insertData(long offset, String arg) throws
DOMException;
Java example
// Daten werden in einen String eingeführt.
boolean fZynisch = true;
// Ein neues Text-Objekt wird in ein CharacterData-Objekt umgewandelt.
CharacterData ndCD = doc.createTextNode("Die Wahrheit ist
da draußen.");
...
// Ist das zynisch?
if (fZynisch) {
ndCD.insertData(15, " nicht");
}
System.out.println(ndCD.getData( ));
replaceData: offset, count, arg
Replaces a substring in the data attribute with the argument arg using the specified offset and count parameters.
Arguments
offset: long
The starting position of the string to replace.
count: long
The number of characters to replace. If
offset+countis greater than or equal to the value of thelengthattribute, the substring starting atoffsetis replaced.arg: DOMString
The replacement string to insert.
The operation replaceData is equivalent to the following code fragment:
cdNode.deleteData(offset, count);
cdNode.insertData(offset, arg);Exceptions
INDEX_SIZE_ERR
The parameter
offsetis invalid, meaning it is not a validdata DOMString.NO_MODIFICATION_ALLOWED_ERR
Raised when the node is read-only.
Java binding
public void replaceData(long offset, long count,
String arg) throws DOMException;
Java example
//Ein neues Text-Objekt wird in ein CharacterData-Objekt umgewandelt.
CharacterData ndCD = doc.createTextNode("Die Wahrheit ist
nicht da draußen.");
// Ersetzen wir die Wahrheit
String strFind = "Wahrheit";
String strReplace = "Katze";
ndCD.replaceData(ndCD.getData().indexOf(strFind), strFind.length( ),
strReplace);
System.out.println(ndCD.getData( ));
substringData: offset, count
Returns a DOMString, containing a subset of the string in the data attribute. The arguments offset and count define this substring. Although the argument offset must specify a valid position in the node data, the substring may extend beyond the end of the data attribute. If so, the method returns everything between the offset position and the end of the data string.
Arguments
offset: unsigned long
The starting position of the substring to read, numbered from zero. A valid
offsetmust therefore be >= 0 and < thelengthattribute of the node.count: unsigned long
Number of characters in the string to read.
Exceptions
INDEX_SIZE_ERR
Raised if the specified parameter
offsetis less than zero or greater than or equal to the value of thelengthattribute, or if thecountparameter is negative.DOMSTRING_SIZE_ERR
Raised if the result string is too large; depending on the system and implementation, the size of a
DOMStringis limited.
Java binding
public String substringData(unsigned long offset, unsigned long count)
throws DOMException;
Java example
// Baue eine Referenz auf das CharacterData-Interface auf.
CharacterData ndCD = doc.createTextNode("Die Wahrheit ist da draußen.");
// Uns interessiert nur die "Wahrheit".
String strWahrheit = ndCD.substringData(4, 8);
System.out.println("Der Substring ist '" + strWahrheit + '\'');
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de