Editing Text in a CDATA, Comment, and Text Node of a DOM Document

This example demonstrates all the available methods to get, set and modify data in a CDATA, comment, or text node.
// Obtain a CDATA, comment, and text node Document doc = createDomDocument(); CDATASection cdataNode = doc.createCDATASection(""); Comment commentNode = doc.createComment(""); Text textNode = doc.createTextNode(""); // All three types of nodes implement the CharacterData interface CharacterData cdata = cdataNode; cdata = commentNode; cdata = textNode; // Set the value of the node cdata.setData("some data"); // Get the length of the text int len = cdata.getLength(); // 9 // Get part of the text int offset = 5; len = 4; String s = cdata.substringData(offset, len); // data // Insert text offset = 5; cdata.insertData(offset, "more "); // some more data // Append text cdata.appendData(" please"); // some more data please // Delete text offset = 0; len = 5; cdata.deleteData(offset, len); // more data please // Replace text String replacement = "now"; offset = 10; len = 6; cdata.replaceData(offset, len, replacement);// more data please

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.