Java XML Encode xmlEncode(String s)

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

Description

xml Encode

License

Apache License

Declaration

public static String xmlEncode(String s) 

Method Source Code

//package com.java2s;
/*/* w ww .j  av  a2s .com*/
   Copyright 2006 OCLC Online Computer Library Center, Inc.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

public class Main {
    public static String xmlEncode(String s) {
        boolean changed = false;
        char c;
        StringBuffer sb = null;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (c < 0xa) {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append("&#x").append(Integer.toHexString(c)).append(';');
            } else if (c == '<') {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append("&lt;");
            } else if (c == '>') {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append("&gt;");
            } else if (c == '"') {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append("&quot;");
            } else if (c == '&') {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append("&amp;");
            } else if (c == '\'') {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append("&apos;");
            } else if (changed)
                sb.append(c);
        }
        if (!changed)
            return s;
        return sb.toString();
    }
}

Related

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