Java tutorial
//package com.java2s; /* This code is part of Freenet. It is distributed under the GNU General * Public License, version 2 (or at your option any later version). See * http://www.gnu.org/ for further details of the GPL. */ import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.Writer; public class Main { public final static void writeSimpleElementCDATALine(Writer w, int deep, String element, String content) throws IOException { writeSimpleElementCDATALine(w, deep, element, new StringReader(content)); } public final static void writeSimpleElementCDATALine(Writer w, int deep, String element, Reader content) throws IOException { if (deep >= 0) writeDeep(w, deep); w.write('<'); w.write(element); w.write('>'); writeEscCData(w, content); w.write('<'); w.write('/'); w.write(element); w.write('>'); w.write('\n'); } public final static void writeDeep(Writer w, int deep) throws IOException { if (deep == 0) return; for (int i = 0; i < deep; i++) { w.write("\t"); } } private static void writeEscCData(Writer w, Reader input) throws IOException { int trigger = 0; w.write("<![CDATA["); int c; while ((c = input.read()) != -1) { if (c == ']') { w.write(']'); trigger++; } else if ((c == '>') && (trigger >= 2)) { w.write("]]><![CDATA[>"); trigger = 0; } else { w.write(c); trigger = 0; } } w.write("]]>"); } }