Java tutorial
//package com.java2s; import java.io.IOException; import java.io.Writer; public class Main { /** * Writes <CODE>string</CODE> into writer, escaping &, ', ", <, and > * with the XML excape strings. */ public static void writeEscapedString(Writer writer, String string) throws IOException { for (int i = 0; i < string.length(); i++) { char c = string.charAt(i); if (c == '<') writer.write("<"); else if (c == '>') writer.write(">"); else if (c == '&') writer.write("&"); else if (c == '\'') writer.write("'"); else if (c == '"') writer.write("""); else writer.write(c); } } }