Java XML String Create toXMLString(String javaString)

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

Description

to XML String

License

Open Source License

Declaration

public static String toXMLString(String javaString) 

Method Source Code

//package com.java2s;
/**/*from   ww  w .j a  v  a2  s  . com*/
 * Copyright 2004-2014 Riccardo Solmi. All rights reserved.
 * This file is part of the Whole Platform.
 *
 * The Whole Platform 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 3 of the License, or
 * (at your option) any later version.
 *
 * The Whole Platform 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 the Whole Platform. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String toXMLString(String javaString) {
        char[] charArray = javaString.toCharArray();
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < charArray.length; i++) {
            final char ch = charArray[i];

            switch (ch) {
            case ' ':
                buffer.append(' ');
                break;
            case '<':
                buffer.append("&lt;");
                break;
            case '>':
                buffer.append("&gt;");
                break;
            case '\'':
                buffer.append("&apos;");
                break;
            case '"':
                buffer.append("&quot;");
                break;
            case '&':
                buffer.append("&amp;");
                break;
            case '\b':
                buffer.append("&#8;");
                break;
            case '\t':
                buffer.append("&#9;");
                break;
            case '\n':
                buffer.append("&#10;");
                break;
            case '\f':
                buffer.append("&#12;");
                break;
            case '\r':
                buffer.append("&#13;");
                break;
            default:
                if (Character.isISOControl(ch) || Character.isWhitespace(ch)) {
                    buffer.append("&#x");
                    buffer.append(charToHex(ch));
                    buffer.append(';');
                } else
                    buffer.append(ch);
                break;
            }
        }
        return buffer.toString();
    }

    public static boolean isWhitespace(CharSequence cs) {
        int count = 0, length = cs.length();
        while (count < length && Character.isWhitespace(cs.charAt(count)))
            count++;
        return count == length;
    }

    public static String charToHex(char c) {
        // Returns hex String representation of char c
        byte hi = (byte) (c >>> 8);
        byte lo = (byte) (c & 0xff);
        return byteToHex(hi) + byteToHex(lo);
    }

    public static String byteToHex(byte b) {
        // Returns hex String representation of byte b
        char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
        return new String(array);
    }
}

Related

  1. toXML(String string)
  2. toXML(String valor)
  3. toXMLString(final boolean[] array)
  4. toXmlString(float value)
  5. toXMLString(String in)
  6. toXMLString(String org)
  7. toXmlString(String s)
  8. toXMLString(String str)
  9. toXMLString(String string)