Java tutorial
//package com.java2s; public class Main { public static String escape(String xmlstr) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < xmlstr.length(); i++) { char c = xmlstr.charAt(i); if (c == '&') buffer.append("&"); else if (c == '<') buffer.append("<"); else if (c == '>') buffer.append(">"); else if (c == '"') buffer.append("""); else if (c == '\'') buffer.append("'"); else buffer.append(c); } return buffer.toString(); } }