Android XML String Escape xmlEscape(String source)

Here you can find the source of xmlEscape(String source)

Description

Escape a string for use in XML.

License

LGPL

Parameter

Parameter Description
source The unescaped string.

Return

An escaped string.

Declaration

public static String xmlEscape(String source) 

Method Source Code

//package com.java2s;
/*/*from   ww  w. ja v a 2  s  . c  o m*/
 * Licensed under Apache License, Version 2.0 or LGPL 2.1, at your option.
 * --
 *
 * Copyright 2010 Rene Treffer
 *
 * 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.
 *
 * --
 *
 * Copyright (C) 2010 Rene Treffer
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

public class Main {
    /**
     * Escape a string for use in XML. This method replaces &, <, >,
     * ' as well as  ".
     * @param source The unescaped string.
     * @return An escaped string.
     */
    public static String xmlEscape(String source) {
        StringBuilder sb = new StringBuilder(source.length()
                + (source.length() / 10));
        for (int i = 0; i < source.length(); i++) {
            char c = source.charAt(i);
            switch ("<>&'\"".indexOf(c)) {
            case 0:
                sb.append("&lt;");
                break;
            case 1:
                sb.append("&gt;");
                break;
            case 2:
                sb.append("&amp;");
                break;
            case 3:
                sb.append("&apos;");
                break;
            case 4:
                sb.append("&quot;");
                break;
            default:
                sb.append(c);
                break;
            }
        }
        return sb.toString();
    }
}

Related

  1. getXmlEncoded(String text)
  2. encodeEntities(String content)
  3. encodeEntities(String content)
  4. encodeXML(final String str)
  5. XmlEscape(String value)
  6. appendPlainText(StringBuilder dest, String src)
  7. buildBuiltinXMLEntityMap()