encode XML String - Android XML

Android examples for XML:XML String

Description

encode XML String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "java2s.com";
        System.out.println(encode(s));
    }/*from w w w. ja v a 2s. co m*/

    public static String encode(String s) {
        String retStr = s;

        if (s.indexOf("<") >= 0)
            retStr = s.replaceAll("<", "&lt;");
        else if (s.indexOf(">") >= 0)
            retStr = s.replaceAll(">", "&gt;");
        else if (s.indexOf("&") >= 0)
            retStr = s.replaceAll("&", "&amp;");
        else if (s.indexOf("\"") >= 0)
            retStr = s.replaceAll("\"", "&quot;");
        else if (s.indexOf("'") >= 0)
            retStr = s.replaceAll("\'", "&apos;");
        return retStr;
    }
}

Related Tutorials