Java tutorial
//package com.java2s; import java.util.*; public class Main { static String entitystr = "><&\'\"\r\n\t"; public static String xmlEncoding(String input) { return xmlEncoding(input, entitystr); } public static String xmlEncoding(String input, String delimiter) { if (input == null || input.equals("")) return input; delimiter += '&'; StringTokenizer tmpst = new StringTokenizer(input, delimiter, true); StringBuffer tmpsb = new StringBuffer(input.length() + 100); String tmps = null; while (tmpst.hasMoreTokens()) { tmps = tmpst.nextToken(); if (tmps.length() == 1 && delimiter.indexOf(tmps) >= 0) { switch (tmps.charAt(0)) { case '<': tmpsb.append("<"); break; case '>': tmpsb.append(">"); break; case '&': tmpsb.append("&"); break; case '\'': tmpsb.append("'"); break; case '\"': tmpsb.append("""); break; case '\n': tmpsb.append(" "); break; case '\r': tmpsb.append(" "); break; case '\t': tmpsb.append("	"); break; } } else { tmpsb.append(tmps); } } return tmpsb.toString(); } }