escape XML String by replace - Java XML

Java examples for XML:XML String Escape

Description

escape XML String by replace

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "java2s.com";
        System.out.println(escape(s));
    }/*from  w  ww .  java2s . c o m*/

    public static String escape(String s) {
        if (s == null) {
            return s;
        }
        return s.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
                .replaceAll(">", "&gt;").replaceAll("'", "&apos;")
                .replaceAll("\"", "&quot;");
    }
}

Related Tutorials