Java tutorial
//package com.java2s; public class Main { /** * Escapes string for XML. * * @param in input string * @return escaped string */ private static String escape(String in) { StringBuilder sb = new StringBuilder(); for (char c : in.toCharArray()) { switch (c) { case '&': sb.append("&"); break; case 0xA0: // \n sb.append(" "); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '"': sb.append("""); break; default: sb.append(c); } } return sb.toString(); } }