Java tutorial
//package com.java2s; public class Main { public static final String START_CDATA = "<![CDATA["; public static final String END_CDATA_RE = "\\]\\]>"; public static final String END_CDATA_REPLACE = "]]>"; /** * CDATA sections can not have a "]]>" in them. This method takes the input and wraps it up in one or more CDATA sections, converting any "]]>" strings into * "]]>". */ public static String wrapStringInCDATA(final String input) { final StringBuffer retValue = new StringBuffer("<![CDATA["); retValue.append(input.replaceAll(END_CDATA_RE, END_CDATA_RE + END_CDATA_REPLACE + START_CDATA)); retValue.append("]]>"); return retValue.toString(); } }