Java tutorial
//package com.java2s; import org.w3c.dom.CharacterData; import org.w3c.dom.Node; public class Main { /** * Get the text from adjacent CharacterData nodes. This will take the text from the passed in node, * and merge it with the text from the next siblings until it reaches a node which is not CharacterData. * * Note that this is necessary during loading, as the result of saving a CDATA node may in fact * be more than one CDATA node in the output. This will happen because any time the substring "]]>" * (the substring to end a CDATA node) appears while outputting the CDATA text, the node transformer * will end the current node at "]]" and start a new one CDATA node to avoid screwing up the output. * * Note: CDATA extends CharacterData (so CDATA != CharacterData) * * @param firstNode the first of [0..n] CharacterData nodes whose text to merge and return. * @param resultBuffer the buffer into which to add the result of merging the CharacterData text. * Warning: anything in this buffer will be overwritten! * @return the first sibling node which is not a CharacterData node, or null is there isn't any. */ public static Node getAdjacentCharacterData(Node firstNode, StringBuilder resultBuffer) { // Clear the result buffer resultBuffer.setLength(0); // merge adjacent CDATA nodes Node codeNode = firstNode; while (codeNode != null && codeNode instanceof CharacterData) { resultBuffer.append(((CharacterData) codeNode).getData()); codeNode = codeNode.getNextSibling(); } return codeNode; } }