Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { public static String toSafeXMLString(String str) { StringBuffer safeXMLStr = new StringBuffer(); for (int i = 0; (str != null) && (i < str.length()); i++) { char ch = str.charAt(i); if (ch == '<') safeXMLStr.append("<"); else if (ch == '>') safeXMLStr.append(">"); else if (ch == '&') safeXMLStr.append("&"); else if (ch == '"') safeXMLStr.append("""); else { safeXMLStr.append(ch); } } return safeXMLStr.toString(); } }