Java XML Encode xmlEncode(String text)

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

Description

xml Encode

License

Open Source License

Declaration

public static String xmlEncode(String text) 

Method Source Code

//package com.java2s;
/*//from w  ww .j  a va 2 s.  c om
 * Copyright (C) 2005-2007 Oleh Hapon ohapon@users.sourceforge.net
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Oleh Hapon
 * Kyiv, UKRAINE
 * ohapon@users.sourceforge.net
 */

public class Main {
    public static String xmlEncode(String text) {
        int length = text.length();
        if (text != null && length > 0) {
            StringBuffer ret = new StringBuffer(length * 12 / 10);

            int last = 0;
            for (int i = 0; i < length; i++) {
                char c = text.charAt(i);
                switch (c) {
                //case ' ' : ret.append("&nbsp;"); break;
                case '&':
                    if (last < i) {
                        ret.append(text.substring(last, i));
                    }
                    last = i + 1;

                    ret.append("&amp;");
                    break;
                case '>':
                    if (last < i) {
                        ret.append(text.substring(last, i));
                    }
                    last = i + 1;

                    ret.append("&gt;");
                    break;
                case '<':
                    if (last < i) {
                        ret.append(text.substring(last, i));
                    }
                    last = i + 1;

                    ret.append("&lt;");
                    break;
                case '\"':
                    if (last < i) {
                        ret.append(text.substring(last, i));
                    }
                    last = i + 1;

                    ret.append("&quot;");
                    break;
                case '\'':
                    if (last < i) {
                        ret.append(text.substring(last, i));
                    }
                    last = i + 1;

                    ret.append("&apos;");
                    break;

                default:
                    break;
                }
            }

            if (last < length) {
                ret.append(text.substring(last));
            }

            return ret.toString();
        }

        return text;
    }
}

Related

  1. xmlEncode(String s)
  2. xmlEncode(String str)
  3. xmlEncode(String str)
  4. xmlEncode(String string)
  5. xmlEncode(String text)
  6. xmlEncode(String text)
  7. xmlEncode(String text, String invalidCharReplacement)
  8. XMLEncode(String value)
  9. xmlEncodeAttr(String src)