// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class XMLEscape {
    public static void main(String[] args) {
        String s1 = "&&&";
        String s2 = xmlConvert1(s1);
        System.out.println(s1 + " : " + s2);
        String s3 = "&&&";
        String s4 = xmlConvert2(s3);
        System.out.println(s3 + " : " + s4);
    }
    public static String xmlConvert1(String tmp)    {
        String s = tmp.replaceAll("&", "&amp;");
        tmp = s.replaceAll("<", "&lt;");
        s = tmp.replaceAll(">", "&gt;");
        return s;
    }
    public static String xmlConvert2(String s) {
        return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }
}
