Java tutorial
//package com.java2s; public class Main { /** * Handles XML encoding of text, e.g. & to & * * @param sText Text to XML encode * @return XML Encoded text */ public static String encodeXMLText(String sText) { StringBuffer sBuff2 = new StringBuffer(sText); StringBuffer sNewBuff = new StringBuffer(); for (int i = 0; i < sBuff2.length(); i++) { char currChar = sBuff2.charAt(i); Character currCharObj = new Character(sBuff2.charAt(i)); if (currChar == '&') { if ((sBuff2.length() - 1 - i) >= 4 && sBuff2.charAt(i + 1) == 'a' && sBuff2.charAt(i + 2) == 'm' && sBuff2.charAt(i + 3) == 'p' && sBuff2.charAt(i + 4) == ';') { i = i + 4; sNewBuff.append("&"); } else { sNewBuff.append("&"); } } else if (currChar == '>') { sNewBuff.append(">"); } else if (currChar == '<') { sNewBuff.append("<"); } else { sNewBuff.append(currChar); } } return sNewBuff.toString(); } }