Java XML Encode xmlEncode(String s)

Here you can find the source of xmlEncode(String s)

Description

xml Encode

License

Open Source License

Declaration

public static String xmlEncode(String s) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

public class Main {
    public static String xmlEncode(String s) {
        if (s == null)
            return null;
        StringBuilder sb = new StringBuilder(s.length() + 16);
        for (int i = 0, n = s.length(); i < n; i++) {
            char c = s.charAt(i);
            switch (c) {
            case '&':
                sb.append("&amp;");
                break;
            case '<':
                sb.append("&lt;");
                break;
            case '\'':
                sb.append("&apos;");
                break;
            case '\"':
                sb.append("&quot;");
                break;
            default:
                if (c >= 128)
                    sb.append(String.format("&#x%04x;", (int) c));
                else
                    sb.append(c);//from w ww.  j  a v  a 2s  . co  m
                break;
            }
        }
        return sb.toString();
    }
}

Related

  1. XMLEncode(final String value)
  2. xmlEncode(String in)
  3. XMLEncode(String name)
  4. xmlEncode(String s)
  5. xmlEncode(String s)
  6. xmlEncode(String s)
  7. xmlEncode(String s)
  8. xmlEncode(String str)
  9. xmlEncode(String str)