Here you can find the source of xmlEncodeCDATA(String src)
public static String xmlEncodeCDATA(String src)
//package com.java2s; /*/*www . j a v a 2 s . com*/ * ==================================================================== * Copyright (c) 2004-2012 TMate Software Ltd. All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://svnkit.com/license.html * If newer versions of this license are posted there, you may use a * newer version instead, at your option. * ==================================================================== */ public class Main { public static String xmlEncodeCDATA(String src) { return xmlEncodeCDATA(src, false); } public static String xmlEncodeCDATA(String src, boolean escapeQuotes) { StringBuffer sb = null; for (int i = 0; i < src.length(); i++) { char ch = src.charAt(i); switch (ch) { case '&': if (sb == null) { sb = createStringBuffer(src, i); } sb.append("&"); break; case '<': if (sb == null) { sb = createStringBuffer(src, i); } sb.append("<"); break; case '>': if (sb == null) { sb = createStringBuffer(src, i); } sb.append(">"); break; case '\r': if (sb == null) { sb = createStringBuffer(src, i); } sb.append(" "); break; case '\"': if (escapeQuotes) { if (sb == null) { sb = createStringBuffer(src, i); } sb.append("""); break; } default: if (sb != null) { sb.append(ch); } } } return sb != null ? sb.toString() : src; } private static StringBuffer createStringBuffer(String src, int length) { StringBuffer sb = new StringBuffer(src.length()); sb.append(src.toCharArray(), 0, length); return sb; } }