Java tutorial
//package com.java2s; public class Main { /** * Converts the raw characters to XML escape characters. * * @param rawContent * @param charset Null when charset is not known, so we assume it's unicode * @param isNoLines * @return escape string */ public static String escapeXML(String rawContent, String charset, boolean isNoLines) { if (rawContent == null) return ""; //$NON-NLS-1$ else { StringBuffer sb = new StringBuffer(); for (int i = 0; i < rawContent.length(); i++) { char ch = rawContent.charAt(i); if (ch == '\'') sb.append("'"); //$NON-NLS-1$ else if (ch == '&') sb.append("&"); //$NON-NLS-1$ else if (ch == '"') sb.append("""); //$NON-NLS-1$ else if (ch == '<') sb.append("<"); //$NON-NLS-1$ else if (ch == '>') sb.append(">"); //$NON-NLS-1$ else if (ch > '~' && charset != null && charSetImpliesAscii(charset)) // TODO - why is hashcode the only way to get the unicode number for the character // in jre 5.0? sb.append("&#x" + Integer.toHexString(new Character(ch).hashCode()).toUpperCase() + ";"); //$NON-NLS-1$ //$NON-NLS-2$ else if (isNoLines) { if (ch == '\r') sb.append("
"); //$NON-NLS-1$ else if (ch != '\n') sb.append(ch); } else sb.append(ch); } return sb.toString(); } } public static boolean charSetImpliesAscii(String charset) { return charset.equals("ISO-8859-1") || charset.equals("US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$ } }