Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { public static String toAttribute(String value) { return "\'" + encodeXML(value) + "\'"; } public static String encodeXML(String value) { if (value == null) return ""; StringBuilder result = new StringBuilder(); for (int i = 0; i < value.length(); ++i) { char c = value.charAt(i); if (c >= 32 && c < 128) switch (c) { case '\'': result.append("'"); break; case '\"': result.append("""); break; case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; default: result.append(c); } else if (c != 0) result.append("&#").append(Integer.toString(c)).append(';'); } return result.toString(); } }