Java tutorial
//package com.java2s; public class Main { public static String encodeXML(String xml) { if (xml == null) return ""; char[] chars = xml.toString().toCharArray(); StringBuffer out = new StringBuffer(); for (char c : chars) switch (c) { case '&': out.append("&"); break; case '<': out.append("<"); break; case '>': out.append(">"); break; case '\"': out.append("""); break; default: out.append(c); } return out.toString(); } }