Java XML String Create toXMLString(String text)

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

Description

Converts a text into a XML string by converting special characters into where corresponding entities.

License

Open Source License

Parameter

Parameter Description
text The string to convert.

Return

The converted string.

Declaration

public static String toXMLString(String text) 

Method Source Code

//package com.java2s;
/*/*w ww  . ja va 2 s  . c  o  m*/
 * Copyright (C) 2002-2006 Stefan Stiller
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    /**
     * Converts a text into a XML string by converting special characters
     * into where corresponding entities.
     *
     * @param text The string to convert.
     * @return The converted string.
     */
    public static String toXMLString(String text) {
        StringBuffer buffer = new StringBuffer();
        int len = text.length();
        for (int i = 0; i < len; i++) {
            char ch = text.charAt(i);
            if ((ch < 0x0020) || (ch > 0x007e)) {
                buffer.append("&#");
                buffer.append((int) ch);
                buffer.append(";");
            } else if (isSpecial(ch))
                buffer.append(getSpecialEntity(ch));
            else
                buffer.append(ch);
        }
        return buffer.toString();
    }

    /**
     * Checks if this character is a special character.
     *
     * @param ch The character.
     * @return Returns <code>true</code> if the character is '&amp;', '&quot;', '&lt;' or '&gt;'
     */
    public static boolean isSpecial(char ch) {
        if (ch == '&')
            return true;
        if (ch == '"')
            return true;
        if (ch == '<')
            return true;
        return ch == '>';
    }

    /**
     * Returns the entity value for special characters.
     *
     * @param ch The character.
     * @return The entity for this character.
     */
    public static String getSpecialEntity(char ch) {
        if (ch == '&')
            return "&amp;";
        if (ch == '"')
            return "&quot;";
        if (ch == '<')
            return "&lt;";
        if (ch == '>')
            return "&gt;";
        return null;
    }
}

Related

  1. toXMLString(String org)
  2. toXmlString(String s)
  3. toXMLString(String str)
  4. toXMLString(String string)
  5. toXMLString(String t)
  6. toXMLUnescapedText(final String text)