Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static final int AMP_CHARACTER = '&'; public static final int LT_CHARACTER = '<'; public static final int GT_CHARACTER = '>'; public static final int QUOTE_CHARACTER = '"'; public static final int APOS_CHARACTER = '\''; private static final String AMP_STRING = "&"; private static final String LT_STRING = "<"; private static final String GT_STRING = ">"; private static final String QUOTE_STRING = "\""; private static final String APOS_STRING = "'"; private static final String AMP_ENTITY = "&"; private static final String LT_ENTITY = "<"; private static final String GT_ENTITY = ">"; private static final String QUOTE_ENTITY = """; private static final String APOS_ENTITY = "'"; public static String convertEntities(String text) { if ("".equals(text.trim())) { return ""; } if (text.indexOf(AMP_CHARACTER) != -1) { text = text.replaceAll(AMP_STRING, AMP_ENTITY); } if (text.indexOf(APOS_CHARACTER) != -1) { text = text.replaceAll(APOS_STRING, APOS_ENTITY); } if (text.indexOf(LT_CHARACTER) != -1) { text = text.replaceAll(LT_STRING, LT_ENTITY); } if (text.indexOf(GT_CHARACTER) != -1) { text = text.replaceAll(GT_STRING, GT_ENTITY); } if (text.indexOf(QUOTE_CHARACTER) != -1) { text = text.replaceAll(QUOTE_STRING, QUOTE_ENTITY); } return text; } }