How can I prevent text that contains "" or ">" from interfering with parsing. Are there other characters I need to be concerned about?
Created May 4, 2012
Robert Quinn A simple XML "encode" should do the trick.
/** * Escape the "special" characters as required for inclusion in XML elements * Replaces all incidences of * & with & * < with < * > with > * " with " * ' with ' * @param s The string to scan * @return String */ public static String escapeStringForXML(String s) { char[] array = s.toCharArray(); StringBuffer sb = new StringBuffer(); for(int i=0; i < array.length; i++) { switch(array[i]) { case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '"': sb.append("""); break; case ''': sb.append("'"); break; default: sb.append(array[i]); } } return sb.toString(); }