Java tutorial
//package com.java2s; // Use and redistribution of this file is governed by the license terms in public class Main { public static String escapeXML(final String text) { final StringBuilder sb = new StringBuilder(); final int len = text.length(); for (int i = 0; i < len; i++) { final char c = text.charAt(i); switch (c) { case 34: sb.append("""); break; case 38: sb.append("&"); break; case 39: sb.append("'"); break; case 60: sb.append("<"); break; case 62: sb.append(">"); break; default: if (c > 0x7F) { sb.append("&#"); sb.append(Integer.toString(c, 10)); sb.append(';'); } else { sb.append(c); } } } return sb.toString(); } }