Java tutorial
//package com.java2s; public class Main { /** * Escape the "special" characters as required for inclusion in XML elements * Replaces all incidences of & with & < with < > with > " with " ' with * ' * * @param s * The string to scan * @return String */ public static String escape(String s) { char[] array = s.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < array.length; i++) { switch (array[i]) { case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '"': sb.append("""); break; case '\'': sb.append("'"); break; default: sb.append(array[i]); } } return sb.toString(); } }