package oboannotator.util; public class XmlEndec { public static final String ENC_AMPS = "&"; public static final String ENC_QUOT = """; public static final String ENC_LT = "<"; public static final String ENC_GT = ">"; public static final char DEC_AMPS = '&'; public static final char DEC_QUOT = '\"'; public static final char DEC_LT = '<'; public static final char DEC_GT = '>'; static public String encodeXML(String myString){ StringBuffer buffer = new StringBuffer(); char chars[] = myString.toCharArray(); for(int i = 0; i< chars.length; i++){ if(chars[i]== DEC_AMPS){ buffer.append("&"); }else if(chars[i]==DEC_GT){ buffer.append(">"); }else if(chars[i]==DEC_LT){ buffer.append("<"); }else if(chars[i]==DEC_QUOT){ buffer.append("""); }else{ buffer.append(chars[i]); } } //System.out.println("Current String encoded is: " + buffer.toString()); return buffer.toString(); } static public String decodeForXML(String myString){ String temp = ""; if(myString.contains(ENC_AMPS) ){ temp = myString.replace(ENC_AMPS, new Character(DEC_AMPS).toString()); //System.out.println("Current ENC_AMPS: " + temp); } if(temp.contains(ENC_GT) ){ myString = temp.replace(ENC_GT, new Character(DEC_GT).toString()); //System.out.println("Current ENC_AMPS: " + myString); } if(myString.contains(ENC_LT) ){ temp = myString.replace(ENC_LT, new Character(DEC_LT).toString()); //System.out.println("Current ENC_AMPS: " + temp); } if(temp.contains(ENC_QUOT) ){ myString = temp.replace(ENC_QUOT, new Character(DEC_QUOT).toString()); } //System.out.println("Current String encoded is: " + myString); return myString; } public static void main(String args[]){ encodeXML("Is this cool or what? \" > < & What say you my precious my love?"); } }